Skip to content

Commit

Permalink
Use ResourceLoader's ClassLoader to load ConfigDataLoaders
Browse files Browse the repository at this point in the history
Fixes gh-34372
  • Loading branch information
wilkinsona committed Feb 24, 2023
1 parent c63e21f commit 72de4a8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ConfigDataEnvironment {
this.environmentUpdateListener = (environmentUpdateListener != null) ? environmentUpdateListener
: ConfigDataEnvironmentUpdateListener.NONE;
this.loaders = new ConfigDataLoaders(logFactory, bootstrapContext,
SpringFactoriesLoader.forDefaultResourceLocation());
SpringFactoriesLoader.forDefaultResourceLocation(resourceLoader.getClassLoader()));
this.contributors = createContributors(binder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package org.springframework.boot.context.config;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -42,10 +46,12 @@
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.mock.env.MockPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -326,6 +332,33 @@ void processAndApplyWhenHasListenerCallsOnSetProfiles(TestInfo info) {
assertThat(listener.getProfiles().getActive()).containsExactly("one", "two", "three");
}

@Test
@SuppressWarnings("rawtypes")
void configDataLoadersAreLoadedUsingClassLoaderFromResourceLoader() {
ResourceLoader resourceLoader = mock(ResourceLoader.class);
ClassLoader classLoader = new ClassLoader() {

@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION.equals(name)) {
return Collections.enumeration(List.of(new File(
"src/test/resources/org/springframework/boot/context/config/separate-class-loader-spring.factories")
.toURI()
.toURL()));
}
return super.getResources(name);
}

};
given(resourceLoader.getClassLoader()).willReturn(classLoader);
TestConfigDataEnvironment configDataEnvironment = new TestConfigDataEnvironment(this.logFactory,
this.bootstrapContext, this.environment, resourceLoader, this.additionalProfiles, null);
assertThat(configDataEnvironment).extracting("loaders.loaders")
.asList()
.extracting((item) -> (Class) item.getClass())
.containsOnly(SeparateClassLoaderConfigDataLoader.class);
}

private String getConfigLocation(TestInfo info) {
return "optional:classpath:" + info.getTestClass().get().getName().replace('.', '/') + "-"
+ info.getTestMethod().get().getName() + ".properties";
Expand Down Expand Up @@ -355,4 +388,14 @@ Binder getConfigDataLocationResolversBinder() {

}

static class SeparateClassLoaderConfigDataLoader implements ConfigDataLoader<ConfigDataResource> {

@Override
public ConfigData load(ConfigDataLoaderContext context, ConfigDataResource resource)
throws IOException, ConfigDataResourceNotFoundException {
return null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.context.config.ConfigDataLoader=\
org.springframework.boot.context.config.ConfigDataEnvironmentTests.SeparateClassLoaderConfigDataLoader

0 comments on commit 72de4a8

Please sign in to comment.