diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextConfiguration.java index 6970e219560..b27ed935154 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,9 +31,7 @@ import org.springframework.core.annotation.Order; /** * Specialized {@link Configuration @Configuration} class that defines configuration * specific for the management context. Configurations should be registered in - * {@code /META-INF/spring.factories} under the - * {@code org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration} - * key. + * {@code /META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}. *

* {@code ManagementContextConfiguration} classes can be ordered using * {@link Order @Order}. Ordering by implementing {@link Ordered} is not supported and diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelector.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelector.java index e840499003e..cc10be2f036 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelector.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelector.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import java.util.Map; 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; import org.springframework.context.annotation.DeferredImportSelector; import org.springframework.core.OrderComparator; import org.springframework.core.Ordered; @@ -36,14 +37,15 @@ import org.springframework.util.StringUtils; /** * Selects configuration classes for the management context configuration. Entries are - * loaded from {@code /META-INF/spring.factories} under the - * {@code org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration} - * key. + * loaded from + * {@code /META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}. * * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Moritz Halbritter * @see ManagementContextConfiguration + * @see ImportCandidates */ @Order(Ordered.LOWEST_PRECEDENCE) class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware { @@ -88,7 +90,10 @@ class ManagementContextConfigurationImportSelector implements DeferredImportSele } protected List loadFactoryNames() { - return SpringFactoriesLoader.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader); + List factoryNames = new ArrayList<>( + SpringFactoriesLoader.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader)); + ImportCandidates.load(ManagementContextConfiguration.class, this.classLoader).forEach(factoryNames::add); + return factoryNames; } @Override diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories index d473f5b7409..c650430800a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories @@ -1,11 +1,2 @@ -org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\ -org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration,\ -org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration,\ -org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration,\ -org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration,\ -org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration,\ -org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration,\ -org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration - org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports new file mode 100644 index 00000000000..404fef621ad --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports @@ -0,0 +1,7 @@ +org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration +org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration +org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration +org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration +org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration +org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration +org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelectorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelectorTests.java index 30823546c17..17818f492ee 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelectorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelectorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,9 @@ package org.springframework.boot.actuate.autoconfigure.web.server; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -24,6 +26,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType; +import org.springframework.boot.context.annotation.ImportCandidates; import org.springframework.core.annotation.Order; import org.springframework.core.type.AnnotationMetadata; @@ -58,6 +61,18 @@ class ManagementContextConfigurationImportSelectorTests { assertThat(imports).containsExactlyInAnyOrder(ChildOnly.class.getName(), A.class.getName()); } + @Test + void selectImportsLoadsFromResources() { + String[] imports = new ManagementContextConfigurationImportSelector() + .selectImports(AnnotationMetadata.introspect(EnableChildContext.class)); + Set expected = new HashSet<>(); + ImportCandidates + .load(ManagementContextConfiguration.class, + ManagementContextConfigurationImportSelectorTests.class.getClassLoader()) + .forEach(expected::add); + assertThat(imports).containsExactlyInAnyOrderElementsOf(expected); + } + private static final class TestManagementContextConfigurationsImportSelector extends ManagementContextConfigurationImportSelector { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java index 9e557d6f030..b31722ca176 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java @@ -23,13 +23,14 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.boot.context.annotation.ImportCandidates; import org.springframework.context.annotation.Import; import org.springframework.core.annotation.AliasFor; /** * Import and apply the specified auto-configuration classes. Applies the same ordering * rules as {@code @EnableAutoConfiguration} but restricts the auto-configuration classes - * to the specified set, rather than consulting {@code spring.factories}. + * to the specified set, rather than consulting {@link ImportCandidates}. *

* Can also be used to {@link #exclude()} specific auto-configuration classes such that * they will never be applied.