Skip to content

Commit dd2ce20

Browse files
committed
SpringFactoriesLoader tolerates whitespace around class names
Issue: SPR-17413
1 parent 83a54db commit dd2ce20

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

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

Lines changed: 4 additions & 4 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;
@@ -139,9 +138,10 @@ private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoad
139138
UrlResource resource = new UrlResource(url);
140139
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
141140
for (Map.Entry<?, ?> entry : properties.entrySet()) {
142-
List<String> factoryClassNames = Arrays.asList(
143-
StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
144-
result.addAll((String) entry.getKey(), factoryClassNames);
141+
String factoryClassName = ((String) entry.getKey()).trim();
142+
for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
143+
result.add(factoryClassName, factoryName.trim());
144+
}
145145
}
146146
}
147147
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)