Skip to content

Commit 38b7ac7

Browse files
committed
GH-1015 - ModulithMetadata obtained from a package now considers @Modulithic annotations.
1 parent 79a80d2 commit 38b7ac7

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

spring-modulith-core/pom.xml

+5-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<groupId>org.springframework</groupId>
4141
<artifactId>spring-core</artifactId>
4242
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-context</artifactId>
47+
</dependency>
4348

4449
<dependency>
4550
<groupId>org.springframework.boot</groupId>
@@ -73,12 +78,6 @@
7378
<optional>true</optional>
7479
</dependency>
7580

76-
<dependency>
77-
<groupId>org.springframework</groupId>
78-
<artifactId>spring-context</artifactId>
79-
<scope>test</scope>
80-
</dependency>
81-
8281
<dependency>
8382
<groupId>org.springframework</groupId>
8483
<artifactId>spring-tx</artifactId>

spring-modulith-core/src/main/java/org/springframework/modulith/core/ModulithMetadata.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
import java.util.List;
1919
import java.util.Optional;
2020
import java.util.function.Supplier;
21+
import java.util.Set;
2122
import java.util.stream.Stream;
2223

24+
import org.springframework.beans.factory.config.BeanDefinition;
25+
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
26+
import org.springframework.core.type.filter.AnnotationTypeFilter;
2327
import org.springframework.modulith.Modulith;
2428
import org.springframework.modulith.Modulithic;
2529
import org.springframework.modulith.core.Types.SpringTypes;
2630
import org.springframework.util.Assert;
31+
import org.springframework.util.ClassUtils;
2732

2833
/**
2934
* Core metadata about the modulithic application.
@@ -56,13 +61,26 @@ public static ModulithMetadata of(Class<?> annotated) {
5661
}
5762

5863
/**
59-
* Creates a new {@link ModulithMetadata} instance for the given package.
64+
* Creates a new {@link ModulithMetadata} instance for the given package. Inspects the package for classes annotated
65+
* with {@link Modulithic} to pick up additional configuration.
6066
*
6167
* @param javaPackage must not be {@literal null} or empty.
6268
* @return will never be {@literal null}.
6369
*/
6470
public static ModulithMetadata of(String javaPackage) {
65-
return SpringBootModulithMetadata.of(javaPackage);
71+
72+
var provider = new ClassPathScanningCandidateComponentProvider(false);
73+
provider.addIncludeFilter(new AnnotationTypeFilter(Modulithic.class));
74+
75+
Set<BeanDefinition> candidates = provider.findCandidateComponents(javaPackage);
76+
77+
if (candidates.size() != 1) {
78+
return SpringBootModulithMetadata.of(javaPackage);
79+
}
80+
81+
var className = candidates.iterator().next().getBeanClassName();
82+
83+
return of(ClassUtils.resolveClassName(className, ModulithMetadata.class.getClassLoader()));
6684
}
6785

6886
/**

spring-modulith-core/src/test/java/empty/EmptyApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
/**
2121
* @author Oliver Drotbohm
2222
*/
23-
@Modulithic
23+
@Modulithic(systemName = "customSystemName")
2424
public class EmptyApplication {}

spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ void returnsUniqueBasePackages() {
7474
assertThat(metadata.getBasePackages()).containsExactly(getClass().getPackageName());
7575
}
7676

77+
@Test // GH-1015
78+
void considersModulithicOnPackageLookup() {
79+
80+
var metadata = ModulithMetadata.of("empty");
81+
82+
assertThat(metadata.getSystemName()).hasValue("customSystemName");
83+
}
84+
7785
@Modulith(additionalPackages = "com.acme.foo", //
7886
sharedModules = "shared.module", //
7987
systemName = "systemName", //

0 commit comments

Comments
 (0)