Skip to content

Commit b093db1

Browse files
committed
Pass ResourceLoader.getClassLoader() to Instantiator
Update calls to `Instantiator` to that they also include `ResourceLoader.getClassLoader()`. Closes gh-27071
1 parent 641dfbd commit b093db1

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataLoaders.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ class ConfigDataLoaders {
5151
* Create a new {@link ConfigDataLoaders} instance.
5252
* @param logFactory the deferred log factory
5353
* @param bootstrapContext the bootstrap context
54-
* @param classLoader the class loader used when loading from {@code spring.factories}
54+
* @param classLoader the class loader used when loading
5555
*/
5656
ConfigDataLoaders(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext,
5757
ClassLoader classLoader) {
58-
this(logFactory, bootstrapContext, SpringFactoriesLoader.loadFactoryNames(ConfigDataLoader.class, classLoader));
58+
this(logFactory, bootstrapContext, classLoader,
59+
SpringFactoriesLoader.loadFactoryNames(ConfigDataLoader.class, classLoader));
5960
}
6061

6162
/**
6263
* Create a new {@link ConfigDataLoaders} instance.
6364
* @param logFactory the deferred log factory
6465
* @param bootstrapContext the bootstrap context
66+
* @param classLoader the class loader used when loading
6567
* @param names the {@link ConfigDataLoader} class names instantiate
6668
*/
6769
ConfigDataLoaders(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext,
68-
List<String> names) {
70+
ClassLoader classLoader, List<String> names) {
6971
this.logger = logFactory.getLog(getClass());
7072
Instantiator<ConfigDataLoader<?>> instantiator = new Instantiator<>(ConfigDataLoader.class,
7173
(availableParameters) -> {
@@ -75,7 +77,7 @@ class ConfigDataLoaders {
7577
availableParameters.add(BootstrapContext.class, bootstrapContext);
7678
availableParameters.add(BootstrapRegistry.class, bootstrapContext);
7779
});
78-
this.loaders = instantiator.instantiate(names);
80+
this.loaders = instantiator.instantiate(classLoader, names);
7981
this.resourceTypes = getResourceTypes(this.loaders);
8082
}
8183

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataLocationResolvers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ConfigDataLocationResolvers {
7777
availableParameters.add(BootstrapContext.class, bootstrapContext);
7878
availableParameters.add(BootstrapRegistry.class, bootstrapContext);
7979
});
80-
this.resolvers = reorder(instantiator.instantiate(names));
80+
this.resolvers = reorder(instantiator.instantiate(resourceLoader.getClassLoader(), names));
8181
}
8282

8383
private List<ConfigDataLocationResolver<?>> reorder(List<ConfigDataLocationResolver<?>> resolvers) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.env;
1818

1919
import java.util.List;
20+
import java.util.function.Function;
2021

2122
import org.springframework.boot.ConfigurableBootstrapContext;
2223
import org.springframework.boot.SpringApplication;
@@ -28,6 +29,7 @@
2829
import org.springframework.context.event.SmartApplicationListener;
2930
import org.springframework.core.Ordered;
3031
import org.springframework.core.env.ConfigurableEnvironment;
32+
import org.springframework.core.io.ResourceLoader;
3133

3234
/**
3335
* {@link SmartApplicationListener} used to trigger {@link EnvironmentPostProcessor
@@ -47,15 +49,14 @@ public class EnvironmentPostProcessorApplicationListener implements SmartApplica
4749

4850
private int order = DEFAULT_ORDER;
4951

50-
private final EnvironmentPostProcessorsFactory postProcessorsFactory;
52+
private final Function<ClassLoader, EnvironmentPostProcessorsFactory> postProcessorsFactory;
5153

5254
/**
5355
* Create a new {@link EnvironmentPostProcessorApplicationListener} with
5456
* {@link EnvironmentPostProcessor} classes loaded via {@code spring.factories}.
5557
*/
5658
public EnvironmentPostProcessorApplicationListener() {
57-
this(EnvironmentPostProcessorsFactory
58-
.fromSpringFactories(EnvironmentPostProcessorApplicationListener.class.getClassLoader()));
59+
this((classLoader) -> EnvironmentPostProcessorsFactory.fromSpringFactories(classLoader), new DeferredLogs());
5960
}
6061

6162
/**
@@ -64,11 +65,11 @@ public EnvironmentPostProcessorApplicationListener() {
6465
* @param postProcessorsFactory the post processors factory
6566
*/
6667
public EnvironmentPostProcessorApplicationListener(EnvironmentPostProcessorsFactory postProcessorsFactory) {
67-
this(postProcessorsFactory, new DeferredLogs());
68+
this((classloader) -> postProcessorsFactory, new DeferredLogs());
6869
}
6970

70-
EnvironmentPostProcessorApplicationListener(EnvironmentPostProcessorsFactory postProcessorsFactory,
71-
DeferredLogs deferredLogs) {
71+
EnvironmentPostProcessorApplicationListener(
72+
Function<ClassLoader, EnvironmentPostProcessorsFactory> postProcessorsFactory, DeferredLogs deferredLogs) {
7273
this.postProcessorsFactory = postProcessorsFactory;
7374
this.deferredLogs = deferredLogs;
7475
}
@@ -96,7 +97,8 @@ public void onApplicationEvent(ApplicationEvent event) {
9697
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
9798
ConfigurableEnvironment environment = event.getEnvironment();
9899
SpringApplication application = event.getSpringApplication();
99-
for (EnvironmentPostProcessor postProcessor : getEnvironmentPostProcessors(event.getBootstrapContext())) {
100+
for (EnvironmentPostProcessor postProcessor : getEnvironmentPostProcessors(application.getResourceLoader(),
101+
event.getBootstrapContext())) {
100102
postProcessor.postProcessEnvironment(environment, application);
101103
}
102104
}
@@ -113,8 +115,11 @@ private void finish() {
113115
this.deferredLogs.switchOverAll();
114116
}
115117

116-
List<EnvironmentPostProcessor> getEnvironmentPostProcessors(ConfigurableBootstrapContext bootstrapContext) {
117-
return this.postProcessorsFactory.getEnvironmentPostProcessors(this.deferredLogs, bootstrapContext);
118+
List<EnvironmentPostProcessor> getEnvironmentPostProcessors(ResourceLoader resourceLoader,
119+
ConfigurableBootstrapContext bootstrapContext) {
120+
ClassLoader classLoader = (resourceLoader != null) ? resourceLoader.getClassLoader() : null;
121+
EnvironmentPostProcessorsFactory postProcessorsFactory = this.postProcessorsFactory.apply(classLoader);
122+
return postProcessorsFactory.getEnvironmentPostProcessors(this.deferredLogs, bootstrapContext);
118123
}
119124

120125
@Override

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLoadersTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class ConfigDataLoadersTests {
5353

5454
@Test
5555
void createWhenLoaderHasLogParameterInjectsLog() {
56-
new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
56+
new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
5757
Arrays.asList(LoggingConfigDataLoader.class.getName()));
5858
}
5959

6060
@Test
6161
void createWhenLoaderHasDeferredLogFactoryParameterInjectsDeferredLogFactory() {
62-
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
62+
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
6363
Arrays.asList(DeferredLogFactoryConfigDataLoader.class.getName()));
6464
assertThat(loaders).extracting("loaders").asList()
6565
.satisfies(this::containsValidDeferredLogFactoryConfigDataLoader);
@@ -73,15 +73,15 @@ private void containsValidDeferredLogFactoryConfigDataLoader(List<?> list) {
7373

7474
@Test
7575
void createWhenLoaderHasBootstrapParametersInjectsBootstrapContext() {
76-
new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
76+
new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
7777
Arrays.asList(BootstrappingConfigDataLoader.class.getName()));
7878
assertThat(this.bootstrapContext.get(String.class)).isEqualTo("boot");
7979
}
8080

8181
@Test
8282
void loadWhenSingleLoaderSupportsLocationReturnsLoadedConfigData() throws Exception {
8383
TestConfigDataResource location = new TestConfigDataResource("test");
84-
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
84+
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
8585
Arrays.asList(TestConfigDataLoader.class.getName()));
8686
ConfigData loaded = loaders.load(this.context, location);
8787
assertThat(getLoader(loaded)).isInstanceOf(TestConfigDataLoader.class);
@@ -90,7 +90,7 @@ void loadWhenSingleLoaderSupportsLocationReturnsLoadedConfigData() throws Except
9090
@Test
9191
void loadWhenMultipleLoadersSupportLocationThrowsException() {
9292
TestConfigDataResource location = new TestConfigDataResource("test");
93-
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
93+
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
9494
Arrays.asList(LoggingConfigDataLoader.class.getName(), TestConfigDataLoader.class.getName()));
9595
assertThatIllegalStateException().isThrownBy(() -> loaders.load(this.context, location))
9696
.withMessageContaining("Multiple loaders found for resource 'test'");
@@ -99,7 +99,7 @@ void loadWhenMultipleLoadersSupportLocationThrowsException() {
9999
@Test
100100
void loadWhenNoLoaderSupportsLocationThrowsException() {
101101
TestConfigDataResource location = new TestConfigDataResource("test");
102-
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
102+
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
103103
Arrays.asList(NonLoadableConfigDataLoader.class.getName()));
104104
assertThatIllegalStateException().isThrownBy(() -> loaders.load(this.context, location))
105105
.withMessage("No loader found for resource 'test'");
@@ -108,7 +108,7 @@ void loadWhenNoLoaderSupportsLocationThrowsException() {
108108
@Test
109109
void loadWhenGenericTypeDoesNotMatchSkipsLoader() throws Exception {
110110
TestConfigDataResource location = new TestConfigDataResource("test");
111-
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext,
111+
ConfigDataLoaders loaders = new ConfigDataLoaders(this.logFactory, this.bootstrapContext, null,
112112
Arrays.asList(OtherConfigDataLoader.class.getName(), SpecificConfigDataLoader.class.getName()));
113113
ConfigData loaded = loaders.load(this.context, location);
114114
assertThat(getLoader(loaded)).isInstanceOf(SpecificConfigDataLoader.class);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListenerTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,19 +50,21 @@ class EnvironmentPostProcessorApplicationListenerTests {
5050
private DefaultBootstrapContext bootstrapContext = spy(new DefaultBootstrapContext());
5151

5252
private EnvironmentPostProcessorApplicationListener listener = new EnvironmentPostProcessorApplicationListener(
53-
EnvironmentPostProcessorsFactory.of(TestEnvironmentPostProcessor.class), this.deferredLogs);
53+
(classLoader) -> EnvironmentPostProcessorsFactory.of(TestEnvironmentPostProcessor.class),
54+
this.deferredLogs);
5455

5556
@Test
5657
void createUsesSpringFactories() {
5758
EnvironmentPostProcessorApplicationListener listener = new EnvironmentPostProcessorApplicationListener();
58-
assertThat(listener.getEnvironmentPostProcessors(this.bootstrapContext)).hasSizeGreaterThan(1);
59+
assertThat(listener.getEnvironmentPostProcessors(null, this.bootstrapContext)).hasSizeGreaterThan(1);
5960
}
6061

6162
@Test
6263
void createWhenHasFactoryUsesFactory() {
6364
EnvironmentPostProcessorApplicationListener listener = new EnvironmentPostProcessorApplicationListener(
6465
EnvironmentPostProcessorsFactory.of(TestEnvironmentPostProcessor.class));
65-
List<EnvironmentPostProcessor> postProcessors = listener.getEnvironmentPostProcessors(this.bootstrapContext);
66+
List<EnvironmentPostProcessor> postProcessors = listener.getEnvironmentPostProcessors(null,
67+
this.bootstrapContext);
6668
assertThat(postProcessors).hasSize(1);
6769
assertThat(postProcessors.get(0)).isInstanceOf(TestEnvironmentPostProcessor.class);
6870
}

0 commit comments

Comments
 (0)