Commit 89b5ece9 authored by Andy Wilkinson's avatar Andy Wilkinson

Use resource loader's class loader in config loading

Previously, classes involved in config loading used a variety of
potentially different class loaders when calling SpringFactoriesLoader.
Some classes would use their own class loader and others would use null
which results in SpringFactoriesLoader's class loader being used.

This commit updates the config loading classes to consistently use the
resource loader's class loader.

Fixes gh-26126
parent f92510e3
...@@ -152,7 +152,7 @@ class ConfigDataEnvironment { ...@@ -152,7 +152,7 @@ class ConfigDataEnvironment {
this.additionalProfiles = additionalProfiles; this.additionalProfiles = additionalProfiles;
this.environmentUpdateListener = (environmentUpdateListener != null) ? environmentUpdateListener this.environmentUpdateListener = (environmentUpdateListener != null) ? environmentUpdateListener
: ConfigDataEnvironmentUpdateListener.NONE; : ConfigDataEnvironmentUpdateListener.NONE;
this.loaders = new ConfigDataLoaders(logFactory, bootstrapContext); this.loaders = new ConfigDataLoaders(logFactory, bootstrapContext, resourceLoader.getClassLoader());
this.contributors = createContributors(binder); this.contributors = createContributors(binder);
} }
......
...@@ -51,9 +51,11 @@ class ConfigDataLoaders { ...@@ -51,9 +51,11 @@ class ConfigDataLoaders {
* Create a new {@link ConfigDataLoaders} instance. * Create a new {@link ConfigDataLoaders} instance.
* @param logFactory the deferred log factory * @param logFactory the deferred log factory
* @param bootstrapContext the bootstrap context * @param bootstrapContext the bootstrap context
* @param classLoader the class loader used when loading from {@code spring.factories}
*/ */
ConfigDataLoaders(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { ConfigDataLoaders(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext,
this(logFactory, bootstrapContext, SpringFactoriesLoader.loadFactoryNames(ConfigDataLoader.class, null)); ClassLoader classLoader) {
this(logFactory, bootstrapContext, SpringFactoriesLoader.loadFactoryNames(ConfigDataLoader.class, classLoader));
} }
/** /**
......
...@@ -54,7 +54,7 @@ class ConfigDataLocationResolvers { ...@@ -54,7 +54,7 @@ class ConfigDataLocationResolvers {
ConfigDataLocationResolvers(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext, ConfigDataLocationResolvers(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext,
Binder binder, ResourceLoader resourceLoader) { Binder binder, ResourceLoader resourceLoader) {
this(logFactory, bootstrapContext, binder, resourceLoader, SpringFactoriesLoader this(logFactory, bootstrapContext, binder, resourceLoader, SpringFactoriesLoader
.loadFactoryNames(ConfigDataLocationResolver.class, ConfigDataLocationResolver.class.getClassLoader())); .loadFactoryNames(ConfigDataLocationResolver.class, resourceLoader.getClassLoader()));
} }
/** /**
......
...@@ -316,7 +316,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, ...@@ -316,7 +316,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
this.placeholdersResolver = new PropertySourcesPlaceholdersResolver(this.environment); this.placeholdersResolver = new PropertySourcesPlaceholdersResolver(this.environment);
this.resourceLoader = (resourceLoader != null) ? resourceLoader : new DefaultResourceLoader(null); this.resourceLoader = (resourceLoader != null) ? resourceLoader : new DefaultResourceLoader(null);
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class,
getClass().getClassLoader()); this.resourceLoader.getClassLoader());
} }
void load() { void load() {
......
...@@ -37,6 +37,7 @@ import org.springframework.boot.context.config.ConfigDataEnvironmentContributors ...@@ -37,6 +37,7 @@ import org.springframework.boot.context.config.ConfigDataEnvironmentContributors
import org.springframework.boot.context.properties.bind.BindException; import org.springframework.boot.context.properties.bind.BindException;
import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.env.MockPropertySource; import org.springframework.mock.env.MockPropertySource;
...@@ -84,8 +85,9 @@ class ConfigDataEnvironmentContributorsTests { ...@@ -84,8 +85,9 @@ class ConfigDataEnvironmentContributorsTests {
this.environment = new MockEnvironment(); this.environment = new MockEnvironment();
this.binder = Binder.get(this.environment); this.binder = Binder.get(this.environment);
ConfigDataLocationResolvers resolvers = new ConfigDataLocationResolvers(this.logFactory, this.bootstrapContext, ConfigDataLocationResolvers resolvers = new ConfigDataLocationResolvers(this.logFactory, this.bootstrapContext,
this.binder, null); this.binder, new DefaultResourceLoader(getClass().getClassLoader()));
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext); ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
getClass().getClassLoader());
this.importer = new ConfigDataImporter(this.logFactory, ConfigDataNotFoundAction.FAIL, resolvers, loaders); this.importer = new ConfigDataImporter(this.logFactory, ConfigDataNotFoundAction.FAIL, resolvers, loaders);
this.activationContext = new ConfigDataActivationContext(CloudPlatform.KUBERNETES, null); this.activationContext = new ConfigDataActivationContext(CloudPlatform.KUBERNETES, null);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment