Skip to content

Commit 1107963

Browse files
committed
SpringFactoriesLoader tolerates whitespace around class names
Issue: SPR-17413 (cherry picked from commit dd2ce20)
1 parent b87ce59 commit 1107963

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.net.URL;
2121
import java.util.ArrayList;
22-
import java.util.Arrays;
2322
import java.util.Collections;
2423
import java.util.Enumeration;
2524
import java.util.List;
@@ -82,9 +81,9 @@ public abstract class SpringFactoriesLoader {
8281
* to obtain all registered factory names.
8382
* @param factoryClass the interface or abstract class representing the factory
8483
* @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
85-
* @see #loadFactoryNames
8684
* @throws IllegalArgumentException if any factory implementation class cannot
8785
* be loaded or if an error occurs while instantiating any factory
86+
* @see #loadFactoryNames
8887
*/
8988
public static <T> List<T> loadFactories(Class<T> factoryClass, @Nullable ClassLoader classLoader) {
9089
Assert.notNull(factoryClass, "'factoryClass' must not be null");
@@ -111,8 +110,8 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, @Nullable ClassLo
111110
* @param factoryClass the interface or abstract class representing the factory
112111
* @param classLoader the ClassLoader to use for loading resources; can be
113112
* {@code null} to use the default
114-
* @see #loadFactories
115113
* @throws IllegalArgumentException if an error occurs while loading factory names
114+
* @see #loadFactories
116115
*/
117116
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
118117
String factoryClassName = factoryClass.getName();
@@ -135,9 +134,10 @@ private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoad
135134
UrlResource resource = new UrlResource(url);
136135
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
137136
for (Map.Entry<?, ?> entry : properties.entrySet()) {
138-
List<String> factoryClassNames = Arrays.asList(
139-
StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
140-
result.addAll((String) entry.getKey(), factoryClassNames);
137+
String factoryClassName = ((String) entry.getKey()).trim();
138+
for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
139+
result.add(factoryClassName, factoryName.trim());
140+
}
141141
}
142142
}
143143
cache.put(classLoader, result);

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)