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:
@@ -70,7 +70,7 @@ public final class SpringFactoriesLoader {
|
|||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
|
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() {
|
private SpringFactoriesLoader() {
|
||||||
@@ -124,11 +124,15 @@ public final class SpringFactoriesLoader {
|
|||||||
* @see #loadFactories
|
* @see #loadFactories
|
||||||
*/
|
*/
|
||||||
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
|
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
|
||||||
|
ClassLoader classLoaderToUse = classLoader;
|
||||||
|
if (classLoaderToUse == null) {
|
||||||
|
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
|
||||||
|
}
|
||||||
String factoryTypeName = factoryType.getName();
|
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);
|
Map<String, List<String>> result = cache.get(classLoader);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
return result;
|
||||||
@@ -136,9 +140,7 @@ public final class SpringFactoriesLoader {
|
|||||||
|
|
||||||
result = new HashMap<>();
|
result = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
Enumeration<URL> urls = (classLoader != null ?
|
Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
|
||||||
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
|
|
||||||
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
|
|
||||||
while (urls.hasMoreElements()) {
|
while (urls.hasMoreElements()) {
|
||||||
URL url = urls.nextElement();
|
URL url = urls.nextElement();
|
||||||
UrlResource resource = new UrlResource(url);
|
UrlResource resource = new UrlResource(url);
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ package org.springframework.core.io.support;
|
|||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@@ -33,6 +35,23 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
|||||||
*/
|
*/
|
||||||
class SpringFactoriesLoaderTests {
|
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
|
@Test
|
||||||
void loadFactoriesWithNoRegisteredImplementations() {
|
void loadFactoriesWithNoRegisteredImplementations() {
|
||||||
List<Integer> factories = SpringFactoriesLoader.loadFactories(Integer.class, null);
|
List<Integer> factories = SpringFactoriesLoader.loadFactories(Integer.class, null);
|
||||||
|
|||||||
Reference in New Issue
Block a user