Use same default ClassLoader in SpringFactoriesLoader
Prior to this commit, the loadFactoryNames() and loadFactories() methods in SpringFactoriesLoader effectively used a different default ClassLoader. This commit ensures that the ClassLoader for SpringFactoriesLoader.class is now consistently used as the default ClassLoader. Closes gh-24992
This commit is contained in:
parent
5abca033d0
commit
859953fe81
|
|
@ -70,7 +70,7 @@ public final class SpringFactoriesLoader {
|
|||
|
||||
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
|
||||
|
||||
private static final Map<ClassLoader, Map<String, List<String>>> cache = new ConcurrentReferenceHashMap<>();
|
||||
static final Map<ClassLoader, Map<String, List<String>>> cache = new ConcurrentReferenceHashMap<>();
|
||||
|
||||
|
||||
private SpringFactoriesLoader() {
|
||||
|
|
@ -124,11 +124,15 @@ public final class SpringFactoriesLoader {
|
|||
* @see #loadFactories
|
||||
*/
|
||||
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
|
||||
ClassLoader classLoaderToUse = classLoader;
|
||||
if (classLoaderToUse == null) {
|
||||
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
|
||||
}
|
||||
String factoryTypeName = factoryType.getName();
|
||||
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
|
||||
return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
|
||||
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
|
||||
Map<String, List<String>> result = cache.get(classLoader);
|
||||
if (result != null) {
|
||||
return result;
|
||||
|
|
@ -136,9 +140,7 @@ public final class SpringFactoriesLoader {
|
|||
|
||||
result = new HashMap<>();
|
||||
try {
|
||||
Enumeration<URL> urls = (classLoader != null ?
|
||||
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
|
||||
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
|
||||
Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
|
||||
while (urls.hasMoreElements()) {
|
||||
URL url = urls.nextElement();
|
||||
UrlResource resource = new UrlResource(url);
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ package org.springframework.core.io.support;
|
|||
import java.lang.reflect.Modifier;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
|
@ -33,6 +35,23 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
|||
*/
|
||||
class SpringFactoriesLoaderTests {
|
||||
|
||||
@BeforeAll
|
||||
static void clearCache() {
|
||||
SpringFactoriesLoader.cache.clear();
|
||||
assertThat(SpringFactoriesLoader.cache).isEmpty();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void checkCache() {
|
||||
assertThat(SpringFactoriesLoader.cache).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadFactoryNames() {
|
||||
List<String> factoryNames = SpringFactoriesLoader.loadFactoryNames(DummyFactory.class, null);
|
||||
assertThat(factoryNames).containsExactlyInAnyOrder(MyDummyFactory1.class.getName(), MyDummyFactory2.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadFactoriesWithNoRegisteredImplementations() {
|
||||
List<Integer> factories = SpringFactoriesLoader.loadFactories(Integer.class, null);
|
||||
|
|
|
|||
Loading…
Reference in New Issue