Polish "Use constructor injection in classes that partipate in @Import"

See gh-51490

Signed-off-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
This commit is contained in:
Andy Wilkinson
2026-09-02 14:16:07 +01:00
parent e161167450
commit d6c2e687b0
4 changed files with 50 additions and 15 deletions
@@ -61,8 +61,15 @@ import com.tngtech.archunit.lang.syntax.elements.ClassesShould;
import com.tngtech.archunit.lang.syntax.elements.GivenMethodsConjunction;
import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.context.annotation.Role;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.ResourceUtils;
/**
@@ -109,6 +116,7 @@ final class ArchitectureRules {
rules.add(autoConfigurationClassesShouldBePublicAndFinal());
rules.add(autoConfigurationClassesShouldHaveNoPublicMembers());
rules.add(testAutoConfigurationClassesShouldBePackagePrivateAndFinal());
rules.add(importParticipantsShouldOnlyImplementAwareInterfacesIfTheyArePublic());
return List.copyOf(rules);
}
@@ -277,6 +285,36 @@ final class ArchitectureRules {
.because(shouldUse("String.toLowerCase(Locale.ROOT)"));
}
private static ArchRule importParticipantsShouldOnlyImplementAwareInterfacesIfTheyArePublic() {
return ArchRuleDefinition.classes()
.that(areImportParticipants())
.and()
.areNotPublic()
.should(notDirectlyImplementAwareInterfaces())
.allowEmptyShould(true);
}
private static DescribedPredicate<JavaClass> areImportParticipants() {
return JavaClass.Predicates.implement(ImportBeanDefinitionRegistrar.class)
.or(JavaClass.Predicates.implement(ImportSelector.class))
.or(JavaClass.Predicates.implement(TypeFilter.class));
}
private static ArchCondition<JavaClass> notDirectlyImplementAwareInterfaces() {
Set<String> awareNames = Set.of(BeanClassLoaderAware.class.getName(), BeanFactoryAware.class.getName(),
EnvironmentAware.class.getName(), ResourceLoaderAware.class.getName());
return ArchCondition
.from(DescribedPredicate.describe("not directly implement BeanClassLoaderAware, BeanFactoryAware,"
+ " EnvironmentAware, or ResourceLoaderAware", (javaClass) -> {
for (JavaClass rawInterface : javaClass.getRawInterfaces()) {
if (awareNames.contains(rawInterface.getFullName())) {
return false;
}
}
return true;
}));
}
private static ArchRule conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(
String annotation) {
return methodsThatAreAnnotatedWith(annotation)
@@ -30,7 +30,6 @@ import org.jspecify.annotations.Nullable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -170,15 +169,13 @@ class ImportsContextCustomizer implements ContextCustomizer {
* {@link ImportSelector} that returns the original test class so that direct
* {@code @Import} annotations are processed.
*/
static class ImportsSelector implements ImportSelector, BeanFactoryAware {
static class ImportsSelector implements ImportSelector {
private static final String[] NO_IMPORTS = {};
@SuppressWarnings("NullAway.Init")
private ConfigurableListableBeanFactory beanFactory;
private final ConfigurableListableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
ImportsSelector(BeanFactory beanFactory) {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
@@ -23,7 +23,6 @@ import java.util.Map;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
import org.springframework.boot.context.annotation.ImportCandidates;
@@ -51,9 +50,13 @@ import org.springframework.util.StringUtils;
* @see ImportCandidates
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware {
class ManagementContextConfigurationImportSelector implements DeferredImportSelector {
private @Nullable ClassLoader classLoader;
private final ClassLoader classLoader;
ManagementContextConfigurationImportSelector(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public String[] selectImports(AnnotationMetadata metadata) {
@@ -98,11 +101,6 @@ class ManagementContextConfigurationImportSelector implements DeferredImportSele
return ImportCandidates.load(ManagementContextConfiguration.class, this.classLoader).getCandidates();
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
/**
* A management configuration class which can be sorted according to {@code @Order}.
*/
@@ -64,7 +64,8 @@ class ManagementContextConfigurationImportSelectorTests {
@Test
void selectImportsLoadsFromResources() {
String[] imports = new ManagementContextConfigurationImportSelector()
String[] imports = new ManagementContextConfigurationImportSelector(
ManagementContextConfigurationImportSelector.class.getClassLoader())
.selectImports(AnnotationMetadata.introspect(EnableChildContext.class));
Set<String> expected = new HashSet<>();
ImportCandidates
@@ -84,6 +85,7 @@ class ManagementContextConfigurationImportSelectorTests {
private final List<String> factoryNames;
private TestManagementContextConfigurationsImportSelector(Class<?>... classes) {
super(ManagementContextConfigurationImportSelector.class.getClassLoader());
this.factoryNames = Stream.of(classes).map(Class::getName).toList();
}