Skip to content

Commit

Permalink
GH-1015 - ModulithMetadata obtained from a package now considers @Mod…
Browse files Browse the repository at this point in the history
…ulithic annotations.
  • Loading branch information
odrotbohm committed Jan 16, 2025
1 parent 79a80d2 commit 38b7ac7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
11 changes: 5 additions & 6 deletions spring-modulith-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -73,12 +78,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.Set;
import java.util.stream.Stream;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.modulith.Modulith;
import org.springframework.modulith.Modulithic;
import org.springframework.modulith.core.Types.SpringTypes;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

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

/**
* Creates a new {@link ModulithMetadata} instance for the given package.
* Creates a new {@link ModulithMetadata} instance for the given package. Inspects the package for classes annotated
* with {@link Modulithic} to pick up additional configuration.
*
* @param javaPackage must not be {@literal null} or empty.
* @return will never be {@literal null}.
*/
public static ModulithMetadata of(String javaPackage) {
return SpringBootModulithMetadata.of(javaPackage);

var provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(Modulithic.class));

Set<BeanDefinition> candidates = provider.findCandidateComponents(javaPackage);

if (candidates.size() != 1) {
return SpringBootModulithMetadata.of(javaPackage);
}

var className = candidates.iterator().next().getBeanClassName();

return of(ClassUtils.resolveClassName(className, ModulithMetadata.class.getClassLoader()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
/**
* @author Oliver Drotbohm
*/
@Modulithic
@Modulithic(systemName = "customSystemName")
public class EmptyApplication {}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ void returnsUniqueBasePackages() {
assertThat(metadata.getBasePackages()).containsExactly(getClass().getPackageName());
}

@Test // GH-1015
void considersModulithicOnPackageLookup() {

var metadata = ModulithMetadata.of("empty");

assertThat(metadata.getSystemName()).hasValue("customSystemName");
}

@Modulith(additionalPackages = "com.acme.foo", //
sharedModules = "shared.module", //
systemName = "systemName", //
Expand Down

0 comments on commit 38b7ac7

Please sign in to comment.