GH-1062 - ApplicationModuleExporter now exposes ApplicationModuleInitializer beans.

This commit is contained in:
Oliver Drotbohm
2025-02-21 22:49:28 +01:00
parent 8fb7f07b4a
commit 19db2339bd
6 changed files with 58 additions and 1 deletions

View File

@@ -301,6 +301,23 @@ public class ApplicationModule implements Comparable<ApplicationModule> {
.toList();
}
/**
* Returns all {@link SpringBean}s assignable to the given type.
*
* @param type must not be {@literal null}.
* @return will never be {@literal null}.
* @since 1.4
*/
public List<SpringBean> getSpringBeans(Class<?> type) {
Assert.notNull(type, "Type must not be null!");
return getSpringBeansInternal().stream() //
.map(it -> SpringBean.of(it, this)) //
.filter(it -> it.isAssignableTo(type))
.toList();
}
/**
* Returns the {@link ArchitecturallyEvidentType} for the given type.
*

View File

@@ -106,6 +106,19 @@ public class SpringBean {
return ArchitecturallyEvidentType.of(type, module.getSpringBeansInternal());
}
/**
* Returns whether the bean is assignable to the given type.
*
* @param type must not be {@literal null}.
* @since 1.4
*/
public boolean isAssignableTo(Class<?> type) {
Assert.notNull(type, "Type must not be null!");
return this.type.isAssignableTo(type);
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)

View File

@@ -30,6 +30,7 @@ import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.modulith.ApplicationModuleInitializer;
import org.springframework.modulith.core.ApplicationModule;
import org.springframework.modulith.core.ApplicationModuleDependency;
import org.springframework.modulith.core.ApplicationModuleIdentifier;
@@ -37,6 +38,7 @@ import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.core.DependencyType;
import org.springframework.modulith.core.NamedInterface;
import org.springframework.modulith.core.NamedInterfaces;
import org.springframework.modulith.core.SpringBean;
import org.springframework.util.Assert;
import com.tngtech.archunit.core.domain.JavaClass;
@@ -137,6 +139,10 @@ public class ApplicationModulesExporter {
if (details.equals(Details.FULL)) {
json.put("namedInterfaces", toNamedInterfaces(module.getNamedInterfaces()));
json.put("initializers", module.getSpringBeans(ApplicationModuleInitializer.class).stream()
.map(SpringBean::getType)
.map(JavaClass::getName)
.toList());
}
json.put("dependencies", module.getDirectDependencies(modules).stream() //

View File

@@ -23,6 +23,13 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-modulith-runtime</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-modulith-test</artifactId>

View File

@@ -16,13 +16,14 @@
package com.acme.myproject.moduleA;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.modulith.ApplicationModuleInitializer;
import org.springframework.stereotype.Component;
/**
* @author Oliver Drotbohm
*/
@Component
public class ServiceComponentA {
public class ServiceComponentA implements ApplicationModuleInitializer {
private final ApplicationEventPublisher publisher;
@@ -33,4 +34,7 @@ public class ServiceComponentA {
public void fireEvent() {
publisher.publishEvent(new SomeEventA("Message"));
}
@Override
public void initialize() {}
}

View File

@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.modulith.core.ApplicationModules;
import com.acme.myproject.Application;
import com.acme.myproject.moduleA.ServiceComponentA;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
@@ -61,4 +62,13 @@ class ApplicationModulesExporterUnitTests {
new ObjectMapper().readTree(EXPORTER.toJson());
});
}
@Test // GH-1062
void fullRenderingListsInitializers() {
var exported = EXPORTER.toFullJson();
assertThat((List<String>) JsonPath.compile("$..initializers[*]").read(exported))
.containsExactly(ServiceComponentA.class.getName());
}
}