SpringFactoriesLoader tolerates whitespace around class names
Issue: SPR-17413
(cherry picked from commit dd2ce20687)
This commit is contained in:
@@ -57,14 +57,14 @@ import org.springframework.util.StringUtils;
|
|||||||
*/
|
*/
|
||||||
public abstract class SpringFactoriesLoader {
|
public abstract class SpringFactoriesLoader {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The location to look for factories.
|
* The location to look for factories.
|
||||||
* <p>Can be present in multiple JAR files.
|
* <p>Can be present in multiple JAR files.
|
||||||
*/
|
*/
|
||||||
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
|
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load and instantiate the factory implementations of the given type from
|
* Load and instantiate the factory implementations of the given type from
|
||||||
@@ -74,9 +74,9 @@ public abstract class SpringFactoriesLoader {
|
|||||||
* to obtain all registered factory names.
|
* to obtain all registered factory names.
|
||||||
* @param factoryClass the interface or abstract class representing the factory
|
* @param factoryClass the interface or abstract class representing the factory
|
||||||
* @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
|
* @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
|
||||||
* @see #loadFactoryNames
|
|
||||||
* @throws IllegalArgumentException if any factory implementation class cannot
|
* @throws IllegalArgumentException if any factory implementation class cannot
|
||||||
* be loaded or if an error occurs while instantiating any factory
|
* be loaded or if an error occurs while instantiating any factory
|
||||||
|
* @see #loadFactoryNames
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
|
public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
|
||||||
Assert.notNull(factoryClass, "'factoryClass' must not be null");
|
Assert.notNull(factoryClass, "'factoryClass' must not be null");
|
||||||
@@ -103,8 +103,8 @@ public abstract class SpringFactoriesLoader {
|
|||||||
* @param factoryClass the interface or abstract class representing the factory
|
* @param factoryClass the interface or abstract class representing the factory
|
||||||
* @param classLoader the ClassLoader to use for loading resources; can be
|
* @param classLoader the ClassLoader to use for loading resources; can be
|
||||||
* {@code null} to use the default
|
* {@code null} to use the default
|
||||||
* @see #loadFactories
|
|
||||||
* @throws IllegalArgumentException if an error occurs while loading factory names
|
* @throws IllegalArgumentException if an error occurs while loading factory names
|
||||||
|
* @see #loadFactories
|
||||||
*/
|
*/
|
||||||
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
|
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
|
||||||
String factoryClassName = factoryClass.getName();
|
String factoryClassName = factoryClass.getName();
|
||||||
@@ -115,14 +115,16 @@ public abstract class SpringFactoriesLoader {
|
|||||||
while (urls.hasMoreElements()) {
|
while (urls.hasMoreElements()) {
|
||||||
URL url = urls.nextElement();
|
URL url = urls.nextElement();
|
||||||
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
|
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
|
||||||
String factoryClassNames = properties.getProperty(factoryClassName);
|
String propertyValue = properties.getProperty(factoryClassName);
|
||||||
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
|
for (String factoryName : StringUtils.commaDelimitedListToStringArray(propertyValue)) {
|
||||||
|
result.add(factoryName.trim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
|
throw new IllegalArgumentException("Unable to load factories from location [" +
|
||||||
"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
|
FACTORIES_RESOURCE_LOCATION + "]", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -33,8 +33,7 @@ public class SpringFactoriesLoaderTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadFactoriesInCorrectOrder() {
|
public void loadFactoriesInCorrectOrder() {
|
||||||
List<DummyFactory> factories = SpringFactoriesLoader
|
List<DummyFactory> factories = SpringFactoriesLoader.loadFactories(DummyFactory.class, null);
|
||||||
.loadFactories(DummyFactory.class, null);
|
|
||||||
assertEquals(2, factories.size());
|
assertEquals(2, factories.size());
|
||||||
assertTrue(factories.get(0) instanceof MyDummyFactory1);
|
assertTrue(factories.get(0) instanceof MyDummyFactory1);
|
||||||
assertTrue(factories.get(1) instanceof MyDummyFactory2);
|
assertTrue(factories.get(1) instanceof MyDummyFactory2);
|
||||||
@@ -46,9 +45,9 @@ public class SpringFactoriesLoaderTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadPackagePrivateFactory() throws Exception {
|
public void loadPackagePrivateFactory() {
|
||||||
List<DummyPackagePrivateFactory> factories = SpringFactoriesLoader
|
List<DummyPackagePrivateFactory> factories =
|
||||||
.loadFactories(DummyPackagePrivateFactory.class, null);
|
SpringFactoriesLoader.loadFactories(DummyPackagePrivateFactory.class, null);
|
||||||
assertEquals(1, factories.size());
|
assertEquals(1, factories.size());
|
||||||
assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0);
|
assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
org.springframework.core.io.support.DummyFactory=\
|
org.springframework.core.io.support.DummyFactory =\
|
||||||
org.springframework.core.io.support.MyDummyFactory2,\
|
org.springframework.core.io.support.MyDummyFactory2, \
|
||||||
org.springframework.core.io.support.MyDummyFactory1
|
org.springframework.core.io.support.MyDummyFactory1
|
||||||
|
|
||||||
java.lang.String=\
|
java.lang.String=\
|
||||||
|
|||||||
Reference in New Issue
Block a user