Skip to content

Commit

Permalink
refactor: Remove obsolete SpringFactoriesLoader#loadFactoryNames
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Nov 30, 2023
1 parent 97cc886 commit 75d8311
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -62,7 +63,7 @@ public String[] selectImports(AnnotationMetadata annotationMetadata) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
List<String> 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", ""))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
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;
Expand Down Expand Up @@ -77,17 +76,12 @@ protected void updateEnvironment() {
// decrypt happen after refresh. The hard coded call to
// ConfigDataEnvironmentPostProcessor.applyTo() is now automated as well.
DeferredLogFactory logFactory = new PassthruDeferredLogFactory();
List<String> classNames = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class,
getClass().getClassLoader());
Instantiator<EnvironmentPostProcessor> 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<EnvironmentPostProcessor> postProcessors = instantiator.instantiate(classNames);
SpringFactoriesLoader.ArgumentResolver argumentResolver = SpringFactoriesLoader.ArgumentResolver.of(DeferredLogFactory.class, logFactory);
argumentResolver = argumentResolver.and(ConfigurableBootstrapContext.class, bootstrapContext);
argumentResolver = argumentResolver.and(BootstrapContext.class, bootstrapContext);
argumentResolver = argumentResolver.and(BootstrapRegistry.class, bootstrapContext);
SpringFactoriesLoader loader = SpringFactoriesLoader.forDefaultResourceLocation(getClass().getClassLoader());
List<EnvironmentPostProcessor> postProcessors = loader.load(EnvironmentPostProcessor.class, argumentResolver);
for (EnvironmentPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessEnvironment(environment, application);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,4 +7,4 @@ org.springframework.cloud.context.test.TestConfigDataLocationResolver

# ConfigData Loaders
org.springframework.boot.context.config.ConfigDataLoader=\
org.springframework.cloud.context.test.TestConfigDataLoader
org.springframework.cloud.context.test.TestConfigDataLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.cloud.bootstrap.TestBootstrapConfiguration
org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration

0 comments on commit 75d8311

Please sign in to comment.