Skip to content

Commit 46bc145

Browse files
committed
SpringFactoriesLoader tolerates whitespace around class names
Issue: SPR-17413 (cherry picked from commit dd2ce20)
1 parent 3a4fd2c commit 46bc145

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@
5757
*/
5858
public abstract class SpringFactoriesLoader {
5959

60-
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
61-
6260
/**
6361
* The location to look for factories.
6462
* <p>Can be present in multiple JAR files.
6563
*/
6664
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
6765

66+
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
67+
6868

6969
/**
7070
* Load and instantiate the factory implementations of the given type from
@@ -74,9 +74,9 @@ public abstract class SpringFactoriesLoader {
7474
* to obtain all registered factory names.
7575
* @param factoryClass the interface or abstract class representing the factory
7676
* @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
77-
* @see #loadFactoryNames
7877
* @throws IllegalArgumentException if any factory implementation class cannot
7978
* be loaded or if an error occurs while instantiating any factory
79+
* @see #loadFactoryNames
8080
*/
8181
public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
8282
Assert.notNull(factoryClass, "'factoryClass' must not be null");
@@ -103,8 +103,8 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader class
103103
* @param factoryClass the interface or abstract class representing the factory
104104
* @param classLoader the ClassLoader to use for loading resources; can be
105105
* {@code null} to use the default
106-
* @see #loadFactories
107106
* @throws IllegalArgumentException if an error occurs while loading factory names
107+
* @see #loadFactories
108108
*/
109109
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
110110
String factoryClassName = factoryClass.getName();
@@ -115,14 +115,16 @@ public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader c
115115
while (urls.hasMoreElements()) {
116116
URL url = urls.nextElement();
117117
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
118-
String factoryClassNames = properties.getProperty(factoryClassName);
119-
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
118+
String propertyValue = properties.getProperty(factoryClassName);
119+
for (String factoryName : StringUtils.commaDelimitedListToStringArray(propertyValue)) {
120+
result.add(factoryName.trim());
121+
}
120122
}
121123
return result;
122124
}
123125
catch (IOException ex) {
124-
throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
125-
"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
126+
throw new IllegalArgumentException("Unable to load factories from location [" +
127+
FACTORIES_RESOURCE_LOCATION + "]", ex);
126128
}
127129
}
128130

spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -33,8 +33,7 @@ public class SpringFactoriesLoaderTests {
3333

3434
@Test
3535
public void loadFactoriesInCorrectOrder() {
36-
List<DummyFactory> factories = SpringFactoriesLoader
37-
.loadFactories(DummyFactory.class, null);
36+
List<DummyFactory> factories = SpringFactoriesLoader.loadFactories(DummyFactory.class, null);
3837
assertEquals(2, factories.size());
3938
assertTrue(factories.get(0) instanceof MyDummyFactory1);
4039
assertTrue(factories.get(1) instanceof MyDummyFactory2);
@@ -46,9 +45,9 @@ public void loadInvalid() {
4645
}
4746

4847
@Test
49-
public void loadPackagePrivateFactory() throws Exception {
50-
List<DummyPackagePrivateFactory> factories = SpringFactoriesLoader
51-
.loadFactories(DummyPackagePrivateFactory.class, null);
48+
public void loadPackagePrivateFactory() {
49+
List<DummyPackagePrivateFactory> factories =
50+
SpringFactoriesLoader.loadFactories(DummyPackagePrivateFactory.class, null);
5251
assertEquals(1, factories.size());
5352
assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0);
5453
}

spring-core/src/test/resources/META-INF/spring.factories

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
org.springframework.core.io.support.DummyFactory=\
2-
org.springframework.core.io.support.MyDummyFactory2,\
1+
org.springframework.core.io.support.DummyFactory =\
2+
org.springframework.core.io.support.MyDummyFactory2, \
33
org.springframework.core.io.support.MyDummyFactory1
44

55
java.lang.String=\

0 commit comments

Comments
 (0)