Skip to content

Commit aeff91c

Browse files
committed
Moved SpringFactoriesLoader to io.support in order to resolve tangle.
1 parent fc859ff commit aeff91c

File tree

7 files changed

+54
-42
lines changed

7 files changed

+54
-42
lines changed

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.core;
17+
package org.springframework.core.io.support;
1818

1919
import java.io.IOException;
2020
import java.net.URL;
@@ -28,8 +28,8 @@
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
3030

31+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3132
import org.springframework.core.io.UrlResource;
32-
import org.springframework.core.io.support.PropertiesLoaderUtils;
3333
import org.springframework.util.Assert;
3434
import org.springframework.util.ClassUtils;
3535
import org.springframework.util.StringUtils;
@@ -44,9 +44,9 @@
4444
* <p>The file should be in {@link Properties} format, where the key is the fully
4545
* qualified interface or abstract class name, and the value is a comma separated list of
4646
* implementations. For instance:
47-
* <pre class="code">
48-
* example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
49-
* </pre>
47+
*
48+
* <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
49+
*
5050
* where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
5151
* {@code MyServiceImpl2} are the two implementations.
5252
*
@@ -60,38 +60,52 @@ public abstract class SpringFactoriesLoader {
6060
/** The location to look for the factories. Can be present in multiple JAR files. */
6161
public static final String DEFAULT_FACTORIES_LOCATION = "META-INF/spring.factories";
6262

63+
/**
64+
* Loads the factory implementations of the given type from the default location.
65+
*
66+
* <p>The returned factories are ordered in accordance with the {@link
67+
* AnnotationAwareOrderComparator}.
68+
*
69+
* @param factoryClass the interface or abstract class representing the factory
70+
* @throws IllegalArgumentException in case of errors
71+
*/
72+
public static <T> List<T> loadFactories(Class<T> factoryClass) {
73+
return loadFactories(factoryClass, null, null);
74+
}
75+
6376
/**
6477
* Loads the factory implementations of the given type from the default location, using
6578
* the given class loader.
6679
*
67-
* <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
80+
* <p>The returned factories are ordered in accordance with the {@link
81+
* AnnotationAwareOrderComparator}.
6882
*
6983
* @param factoryClass the interface or abstract class representing the factory
70-
* @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
71-
* default
84+
* @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
85+
* default
7286
* @throws IllegalArgumentException in case of errors
7387
*/
7488
public static <T> List<T> loadFactories(Class<T> factoryClass,
75-
ClassLoader classLoader) {
89+
ClassLoader classLoader) {
7690
return loadFactories(factoryClass, classLoader, null);
7791
}
7892

7993
/**
8094
* Loads the factory implementations of the given type from the given location, using the
8195
* given class loader.
8296
*
83-
* <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
97+
* <p>The returned factories are ordered in accordance with the {@link
98+
* AnnotationAwareOrderComparator}.
8499
*
85-
* @param factoryClass the interface or abstract class representing the factory
86-
* @param classLoader the ClassLoader to use for loading, can be {@code null} to
87-
* use the default
100+
* @param factoryClass the interface or abstract class representing the factory
101+
* @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
102+
* default
88103
* @param factoriesLocation the factories file location, can be {@code null} to use the
89-
* {@linkplain #DEFAULT_FACTORIES_LOCATION default}
104+
* {@linkplain #DEFAULT_FACTORIES_LOCATION default}
90105
* @throws IllegalArgumentException in case of errors
91106
*/
92107
public static <T> List<T> loadFactories(Class<T> factoryClass,
93-
ClassLoader classLoader,
94-
String factoriesLocation) {
108+
ClassLoader classLoader, String factoriesLocation) {
95109
Assert.notNull(factoryClass, "'factoryClass' must not be null");
96110

97111
if (classLoader == null) {
@@ -114,22 +128,21 @@ public static <T> List<T> loadFactories(Class<T> factoryClass,
114128
result.add(instantiateFactory(factoryName, factoryClass, classLoader));
115129
}
116130

117-
Collections.sort(result, new OrderComparator());
131+
Collections.sort(result, new AnnotationAwareOrderComparator());
118132

119133
return result;
120134
}
121135

122136
private static List<String> loadFactoryNames(Class<?> factoryClass,
123-
ClassLoader classLoader,
124-
String factoriesLocation) {
137+
ClassLoader classLoader, String factoriesLocation) {
125138

126139
String factoryClassName = factoryClass.getName();
127140

128141
try {
129142
List<String> result = new ArrayList<String>();
130143
Enumeration<URL> urls = classLoader.getResources(factoriesLocation);
131144
while (urls.hasMoreElements()) {
132-
URL url = (URL) urls.nextElement();
145+
URL url = urls.nextElement();
133146
Properties properties =
134147
PropertiesLoaderUtils.loadProperties(new UrlResource(url));
135148
String factoryClassNames = properties.getProperty(factoryClassName);
@@ -150,8 +163,7 @@ private static List<String> loadFactoryNames(Class<?> factoryClass,
150163

151164
@SuppressWarnings("unchecked")
152165
private static <T> T instantiateFactory(String instanceClassName,
153-
Class<T> factoryClass,
154-
ClassLoader classLoader) {
166+
Class<T> factoryClass, ClassLoader classLoader) {
155167
try {
156168
Class<?> instanceClass = ClassUtils.forName(instanceClassName, classLoader);
157169
if (!factoryClass.isAssignableFrom(instanceClass)) {

spring-core/src/test/java/org/springframework/core/DummyFactory.java renamed to spring-core/src/test/java/org/springframework/core/io/support/DummyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.core;
17+
package org.springframework.core.io.support;
1818

1919
/**
2020
* Used by {@link SpringFactoriesLoaderTests}

spring-core/src/test/java/org/springframework/core/MyDummyFactory1.java renamed to spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory1.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.core;
17+
package org.springframework.core.io.support;
18+
19+
import org.springframework.core.annotation.Order;
1820

1921
/**
2022
* Used by {@link SpringFactoriesLoaderTests}
2123
*
2224
* @author Arjen Poutsma
2325
*/
24-
public class MyDummyFactory1 implements DummyFactory, Ordered {
25-
26-
public int getOrder() {
27-
return 1;
28-
}
26+
@Order(1)
27+
public class MyDummyFactory1 implements DummyFactory {
2928

3029
public String getString() {
3130
return "Foo";

spring-core/src/test/java/org/springframework/core/MyDummyFactory2.java renamed to spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory2.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.core;
17+
package org.springframework.core.io.support;
18+
19+
import org.springframework.core.annotation.Order;
1820

1921
/**
20-
* Used by {@link org.springframework.core.SpringFactoriesLoaderTests}
22+
* Used by {@link SpringFactoriesLoaderTests}
2123
*
2224
* @author Arjen Poutsma
2325
*/
24-
public class MyDummyFactory2 implements DummyFactory, Ordered {
25-
26-
public int getOrder() {
27-
return 2;
28-
}
26+
@Order(2)
27+
public class MyDummyFactory2 implements DummyFactory {
2928

3029
public String getString() {
3130
return "Bar";

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.core;
17+
package org.springframework.core.io.support;
1818

1919
import java.util.List;
2020

@@ -25,11 +25,13 @@
2525
/** @author Arjen Poutsma */
2626
public class SpringFactoriesLoaderTests {
2727

28+
private static final String FACTORIES_LOCATION =
29+
"org/springframework/core/io/support/springFactoriesLoaderTests.properties";
30+
2831
@Test
2932
public void loadFactories() {
3033
List<DummyFactory> factories = SpringFactoriesLoader
31-
.loadFactories(DummyFactory.class, null,
32-
"org/springframework/core/springFactoriesLoaderTests.properties");
34+
.loadFactories(DummyFactory.class, null, FACTORIES_LOCATION);
3335

3436
assertEquals(2, factories.size());
3537
assertTrue(factories.get(0) instanceof MyDummyFactory1);
@@ -39,7 +41,7 @@ public void loadFactories() {
3941
@Test(expected = IllegalArgumentException.class)
4042
public void loadInvalid() {
4143
SpringFactoriesLoader.loadFactories(String.class, null,
42-
"org/springframework/core/springFactoriesLoaderTests.properties");
44+
FACTORIES_LOCATION);
4345
}
4446

4547
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.core.io.support.DummyFactory=org.springframework.core.io.support.MyDummyFactory2,org.springframework.core.io.support.MyDummyFactory1
2+
java.lang.String=org.springframework.core.io.support.MyDummyFactory1

spring-core/src/test/resources/org/springframework/core/springFactoriesLoaderTests.properties

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)