Skip to content

Commit 889c849

Browse files
committed
GH-348 - Avoid eager reference to runtime artifact from core starter.
Removed the inclusion of the spring-modulith-runtime artifact from the …-starter-core one to avoid issues in native images in a Spring Modulith default setup. As the artifact is still needed to support ApplicationModuleInitializer implementations, we now explicitly check for the presence of those and verify the artifact is actually available and hint the user at explicitly adding it if missing.
1 parent 576b8f5 commit 889c849

File tree

4 files changed

+119
-7
lines changed

4 files changed

+119
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.core.config;
17+
18+
import java.util.Arrays;
19+
20+
import org.springframework.beans.BeansException;
21+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
22+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
23+
import org.springframework.boot.autoconfigure.AutoConfiguration;
24+
import org.springframework.modulith.ApplicationModuleInitializer;
25+
import org.springframework.util.ClassUtils;
26+
27+
/**
28+
* A {@link BeanFactoryPostProcessor} verifying that the {@code spring-modulith-runtime} artifact is on the classpath in
29+
* case any beans implementing {@link ApplicationModuleInitializer} are found in the
30+
* {@code org.springframework.context.ApplicationContext}.
31+
*
32+
* @author Oliver Drotbohm
33+
* @since 1.1
34+
*/
35+
@AutoConfiguration
36+
class ApplicationModuleInitializerRuntimeVerification implements BeanFactoryPostProcessor {
37+
38+
private static final String ARTIFACT_MISSING = "Detected bean(s) (%s) implementing ApplicationModuleInitializer but Spring Modulith Runtime artifact not on the classpath! Please add org.springframework.modulith:spring-modulith-runtime to your project!";
39+
40+
/*
41+
* (non-Javadoc)
42+
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
43+
*/
44+
@Override
45+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
46+
47+
var initializerBeanNames = beanFactory.getBeanNamesForType(ApplicationModuleInitializer.class);
48+
49+
if (initializerBeanNames.length == 0) {
50+
return;
51+
}
52+
53+
if (!ClassUtils.isPresent("org.springframework.modulith.runtime.ApplicationRuntime", getClass().getClassLoader())) {
54+
throw new IllegalStateException(ARTIFACT_MISSING.formatted(Arrays.toString(initializerBeanNames)));
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.modulith.core.config.ApplicationModuleInitializerRuntimeVerification
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.core.config;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.modulith.ApplicationModuleInitializer;
25+
26+
/**
27+
* Tests for {@link ApplicationModuleInitializerRuntimeVerification}.
28+
*
29+
* @author Oliver Drotbohm
30+
* @since 1.1
31+
*/
32+
class ApplicationModuleInitializerRuntimeVerificationTests {
33+
34+
@Test // GH-348
35+
void startsWithoutApplicationModuleInititalizersPresent() {
36+
37+
new ApplicationContextRunner()
38+
.withConfiguration(AutoConfigurations.of(ApplicationModuleInitializerRuntimeVerification.class))
39+
.run(ctx -> {
40+
assertThat(ctx).hasNotFailed();
41+
});
42+
}
43+
44+
@Test // GH-348
45+
void rejectsPresenceOfAppliactionModuleInitializerBeanIfRuntimeArtifactIsMissing() {
46+
47+
new ApplicationContextRunner()
48+
.withConfiguration(AutoConfigurations.of(ApplicationModuleInitializerRuntimeVerification.class))
49+
.withBean(ApplicationModuleInitializer.class, () -> mock(ApplicationModuleInitializer.class))
50+
.run(ctx -> {
51+
52+
assertThat(ctx)
53+
.hasFailed()
54+
.getFailure()
55+
.isInstanceOf(IllegalStateException.class)
56+
.hasMessageContaining("ApplicationModuleInitializer") // Type
57+
.hasMessageContaining("applicationModuleInitializer") // Bean name
58+
.hasMessageContaining("spring-modulith-runtime"); // Missing artifact
59+
});
60+
}
61+
}

spring-modulith-starters/spring-modulith-starter-core/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@
3737
<version>1.1.0-SNAPSHOT</version>
3838
</dependency>
3939

40-
<dependency>
41-
<groupId>org.springframework.modulith</groupId>
42-
<artifactId>spring-modulith-runtime</artifactId>
43-
<version>1.1.0-SNAPSHOT</version>
44-
<scope>runtime</scope>
45-
</dependency>
46-
4740
<dependency>
4841
<groupId>org.springframework.boot</groupId>
4942
<artifactId>spring-boot-starter</artifactId>

0 commit comments

Comments
 (0)