From 47bb05f611f4fcb735b2e6cd4d6a6d451485955f Mon Sep 17 00:00:00 2001 From: livk Date: Thu, 30 Nov 2023 16:43:26 +0800 Subject: [PATCH] refactor: Remove obsolete SpringFactoriesLoader#loadFactoryNames --- .../bootstrap/BootstrapImportSelector.java | 11 ++++++----- .../refresh/ConfigDataContextRefresher.java | 18 +++--------------- .../main/resources/META-INF/spring.factories | 6 ------ ...ud.bootstrap.BootstrapConfiguration.imports | 4 ++++ .../test/resources/META-INF/spring.factories | 7 +------ ...ud.bootstrap.BootstrapConfiguration.imports | 2 ++ 6 files changed, 16 insertions(+), 32 deletions(-) create mode 100644 spring-cloud-context/src/main/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports create mode 100644 spring-cloud-context/src/test/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapImportSelector.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapImportSelector.java index 62994c622..29ff279cd 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapImportSelector.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapImportSelector.java @@ -24,12 +24,12 @@ import java.util.List; import java.util.Map; +import org.springframework.boot.context.annotation.ImportCandidates; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.DeferredImportSelector; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; -import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.style.ToStringCreator; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; @@ -38,9 +38,10 @@ import org.springframework.util.StringUtils; /** - * This class uses {@link SpringFactoriesLoader} to load {@link BootstrapConfiguration} - * entries from {@code spring.factories}. The classes are then loaded so they can be - * sorted using {@link AnnotationAwareOrderComparator#sort(List)}. This class is a + * This class uses {@link ImportCandidates} to load {@link BootstrapConfiguration} entries + * from {@code org.springframework.cloud.bootstrap.BootstrapConfiguration.imports}. + * The classes are then loaded so they can be sorted using + * {@link AnnotationAwareOrderComparator#sort(List)}. This class is a * {@link DeferredImportSelector} so {@code @Conditional} annotations on imported classes * are supported. * @@ -62,7 +63,7 @@ public String[] selectImports(AnnotationMetadata annotationMetadata) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Use names and ensure unique to protect against duplicates List names = new ArrayList<>( - SpringFactoriesLoader.loadFactoryNames(BootstrapConfiguration.class, classLoader)); + ImportCandidates.load(BootstrapConfiguration.class, classLoader).getCandidates()); names.addAll(Arrays.asList(StringUtils .commaDelimitedListToStringArray(this.environment.getProperty("spring.cloud.bootstrap.sources", "")))); diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java index 08fc45cee..19b80f7ac 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java @@ -22,14 +22,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.BootstrapContext; -import org.springframework.boot.BootstrapRegistry; import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.env.EnvironmentPostProcessorsFactory; import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.boot.util.Instantiator; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.context.config.ContextRefreshedWithApplicationEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; @@ -38,7 +36,6 @@ import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; -import org.springframework.core.io.support.SpringFactoriesLoader; /** * @author Dave Syer @@ -77,17 +74,8 @@ protected void updateEnvironment() { // decrypt happen after refresh. The hard coded call to // ConfigDataEnvironmentPostProcessor.applyTo() is now automated as well. DeferredLogFactory logFactory = new PassthruDeferredLogFactory(); - List classNames = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, - getClass().getClassLoader()); - Instantiator instantiator = new Instantiator<>(EnvironmentPostProcessor.class, - (parameters) -> { - parameters.add(DeferredLogFactory.class, logFactory); - parameters.add(Log.class, logFactory::getLog); - parameters.add(ConfigurableBootstrapContext.class, bootstrapContext); - parameters.add(BootstrapContext.class, bootstrapContext); - parameters.add(BootstrapRegistry.class, bootstrapContext); - }); - List postProcessors = instantiator.instantiate(classNames); + EnvironmentPostProcessorsFactory environmentPostProcessorsFactory = EnvironmentPostProcessorsFactory.fromSpringFactories(getClass().getClassLoader()); + List postProcessors = environmentPostProcessorsFactory.getEnvironmentPostProcessors(logFactory, bootstrapContext); for (EnvironmentPostProcessor postProcessor : postProcessors) { postProcessor.postProcessEnvironment(environment, application); } diff --git a/spring-cloud-context/src/main/resources/META-INF/spring.factories b/spring-cloud-context/src/main/resources/META-INF/spring.factories index 6d43199ba..0cee8d814 100644 --- a/spring-cloud-context/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-context/src/main/resources/META-INF/spring.factories @@ -3,12 +3,6 @@ org.springframework.context.ApplicationListener=\ org.springframework.cloud.bootstrap.BootstrapApplicationListener,\ org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,\ org.springframework.cloud.context.restart.RestartListener -# Spring Cloud Bootstrap components -org.springframework.cloud.bootstrap.BootstrapConfiguration=\ -org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\ -org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\ -org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ -org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration # Spring Boot BootstrapRegistryInitializer org.springframework.boot.BootstrapRegistryInitializer=\ org.springframework.cloud.bootstrap.RefreshBootstrapRegistryInitializer,\ diff --git a/spring-cloud-context/src/main/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports b/spring-cloud-context/src/main/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports new file mode 100644 index 000000000..9ef158d4f --- /dev/null +++ b/spring-cloud-context/src/main/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports @@ -0,0 +1,4 @@ +org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration +org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration +org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration +org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration diff --git a/spring-cloud-context/src/test/resources/META-INF/spring.factories b/spring-cloud-context/src/test/resources/META-INF/spring.factories index ca038dade..3556e9d10 100644 --- a/spring-cloud-context/src/test/resources/META-INF/spring.factories +++ b/spring-cloud-context/src/test/resources/META-INF/spring.factories @@ -1,8 +1,3 @@ -# Bootstrap components -org.springframework.cloud.bootstrap.BootstrapConfiguration=\ -org.springframework.cloud.bootstrap.TestBootstrapConfiguration,\ -org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration - org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.cloud.context.test.TestEnvPostProcessor @@ -12,4 +7,4 @@ org.springframework.cloud.context.test.TestConfigDataLocationResolver # ConfigData Loaders org.springframework.boot.context.config.ConfigDataLoader=\ -org.springframework.cloud.context.test.TestConfigDataLoader \ No newline at end of file +org.springframework.cloud.context.test.TestConfigDataLoader diff --git a/spring-cloud-context/src/test/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports b/spring-cloud-context/src/test/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports new file mode 100644 index 000000000..ec02d2b36 --- /dev/null +++ b/spring-cloud-context/src/test/resources/META-INF/spring/org.springframework.cloud.bootstrap.BootstrapConfiguration.imports @@ -0,0 +1,2 @@ +org.springframework.cloud.bootstrap.TestBootstrapConfiguration +org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration