diff --git a/spring-core/src/main/java/org/springframework/core/SpringFactoriesLoader.java b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java similarity index 80% rename from spring-core/src/main/java/org/springframework/core/SpringFactoriesLoader.java rename to spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java index 0954d8cf4f1..6a67ec7e3a7 100644 --- a/spring-core/src/main/java/org/springframework/core/SpringFactoriesLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.core; +package org.springframework.core.io.support; import java.io.IOException; import java.net.URL; @@ -28,8 +28,8 @@ import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.io.UrlResource; -import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -44,9 +44,9 @@ import org.springframework.util.StringUtils; *

The file should be in {@link Properties} format, where the key is the fully * qualified interface or abstract class name, and the value is a comma separated list of * implementations. For instance: - *

- * example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
- * 
+ * + *
example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
+ * * where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and * {@code MyServiceImpl2} are the two implementations. * @@ -60,19 +60,33 @@ public abstract class SpringFactoriesLoader { /** The location to look for the factories. Can be present in multiple JAR files. */ public static final String DEFAULT_FACTORIES_LOCATION = "META-INF/spring.factories"; + /** + * Loads the factory implementations of the given type from the default location. + * + *

The returned factories are ordered in accordance with the {@link + * AnnotationAwareOrderComparator}. + * + * @param factoryClass the interface or abstract class representing the factory + * @throws IllegalArgumentException in case of errors + */ + public static List loadFactories(Class factoryClass) { + return loadFactories(factoryClass, null, null); + } + /** * Loads the factory implementations of the given type from the default location, using * the given class loader. * - *

The returned factories are ordered in accordance with the {@link OrderComparator}. + *

The returned factories are ordered in accordance with the {@link + * AnnotationAwareOrderComparator}. * * @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 * @throws IllegalArgumentException in case of errors */ public static List loadFactories(Class factoryClass, - ClassLoader classLoader) { + ClassLoader classLoader) { return loadFactories(factoryClass, classLoader, null); } @@ -80,18 +94,18 @@ public abstract class SpringFactoriesLoader { * Loads the factory implementations of the given type from the given location, using the * given class loader. * - *

The returned factories are ordered in accordance with the {@link OrderComparator}. + *

The returned factories are ordered in accordance with the {@link + * AnnotationAwareOrderComparator}. * - * @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 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 factoriesLocation the factories file location, can be {@code null} to use the - * {@linkplain #DEFAULT_FACTORIES_LOCATION default} + * {@linkplain #DEFAULT_FACTORIES_LOCATION default} * @throws IllegalArgumentException in case of errors */ public static List loadFactories(Class factoryClass, - ClassLoader classLoader, - String factoriesLocation) { + ClassLoader classLoader, String factoriesLocation) { Assert.notNull(factoryClass, "'factoryClass' must not be null"); if (classLoader == null) { @@ -114,14 +128,13 @@ public abstract class SpringFactoriesLoader { result.add(instantiateFactory(factoryName, factoryClass, classLoader)); } - Collections.sort(result, new OrderComparator()); + Collections.sort(result, new AnnotationAwareOrderComparator()); return result; } private static List loadFactoryNames(Class factoryClass, - ClassLoader classLoader, - String factoriesLocation) { + ClassLoader classLoader, String factoriesLocation) { String factoryClassName = factoryClass.getName(); @@ -129,7 +142,7 @@ public abstract class SpringFactoriesLoader { List result = new ArrayList(); Enumeration urls = classLoader.getResources(factoriesLocation); while (urls.hasMoreElements()) { - URL url = (URL) urls.nextElement(); + URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); @@ -150,8 +163,7 @@ public abstract class SpringFactoriesLoader { @SuppressWarnings("unchecked") private static T instantiateFactory(String instanceClassName, - Class factoryClass, - ClassLoader classLoader) { + Class factoryClass, ClassLoader classLoader) { try { Class instanceClass = ClassUtils.forName(instanceClassName, classLoader); if (!factoryClass.isAssignableFrom(instanceClass)) { diff --git a/spring-core/src/test/java/org/springframework/core/DummyFactory.java b/spring-core/src/test/java/org/springframework/core/io/support/DummyFactory.java similarity index 94% rename from spring-core/src/test/java/org/springframework/core/DummyFactory.java rename to spring-core/src/test/java/org/springframework/core/io/support/DummyFactory.java index d19257a79aa..12d98292e56 100644 --- a/spring-core/src/test/java/org/springframework/core/DummyFactory.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/DummyFactory.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.core; +package org.springframework.core.io.support; /** * Used by {@link SpringFactoriesLoaderTests} diff --git a/spring-core/src/test/java/org/springframework/core/MyDummyFactory1.java b/spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory1.java similarity index 82% rename from spring-core/src/test/java/org/springframework/core/MyDummyFactory1.java rename to spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory1.java index f8d2a122f05..d1b6fa034df 100644 --- a/spring-core/src/test/java/org/springframework/core/MyDummyFactory1.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory1.java @@ -14,18 +14,17 @@ * limitations under the License. */ -package org.springframework.core; +package org.springframework.core.io.support; + +import org.springframework.core.annotation.Order; /** * Used by {@link SpringFactoriesLoaderTests} * * @author Arjen Poutsma */ -public class MyDummyFactory1 implements DummyFactory, Ordered { - - public int getOrder() { - return 1; - } +@Order(1) +public class MyDummyFactory1 implements DummyFactory { public String getString() { return "Foo"; diff --git a/spring-core/src/test/java/org/springframework/core/MyDummyFactory2.java b/spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory2.java similarity index 77% rename from spring-core/src/test/java/org/springframework/core/MyDummyFactory2.java rename to spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory2.java index 0a7d5f6245c..4872d0053ee 100644 --- a/spring-core/src/test/java/org/springframework/core/MyDummyFactory2.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/MyDummyFactory2.java @@ -14,18 +14,17 @@ * limitations under the License. */ -package org.springframework.core; +package org.springframework.core.io.support; + +import org.springframework.core.annotation.Order; /** - * Used by {@link org.springframework.core.SpringFactoriesLoaderTests} + * Used by {@link SpringFactoriesLoaderTests} * * @author Arjen Poutsma */ -public class MyDummyFactory2 implements DummyFactory, Ordered { - - public int getOrder() { - return 2; - } +@Order(2) +public class MyDummyFactory2 implements DummyFactory { public String getString() { return "Bar"; diff --git a/spring-core/src/test/java/org/springframework/core/SpringFactoriesLoaderTests.java b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java similarity index 82% rename from spring-core/src/test/java/org/springframework/core/SpringFactoriesLoaderTests.java rename to spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java index 07fb50fbc61..98510eb960e 100644 --- a/spring-core/src/test/java/org/springframework/core/SpringFactoriesLoaderTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.core; +package org.springframework.core.io.support; import java.util.List; @@ -25,11 +25,13 @@ import org.junit.Test; /** @author Arjen Poutsma */ public class SpringFactoriesLoaderTests { + private static final String FACTORIES_LOCATION = + "org/springframework/core/io/support/springFactoriesLoaderTests.properties"; + @Test public void loadFactories() { List factories = SpringFactoriesLoader - .loadFactories(DummyFactory.class, null, - "org/springframework/core/springFactoriesLoaderTests.properties"); + .loadFactories(DummyFactory.class, null, FACTORIES_LOCATION); assertEquals(2, factories.size()); assertTrue(factories.get(0) instanceof MyDummyFactory1); @@ -39,7 +41,7 @@ public class SpringFactoriesLoaderTests { @Test(expected = IllegalArgumentException.class) public void loadInvalid() { SpringFactoriesLoader.loadFactories(String.class, null, - "org/springframework/core/springFactoriesLoaderTests.properties"); + FACTORIES_LOCATION); } } diff --git a/spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties b/spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties new file mode 100644 index 00000000000..d3e700fe87e --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties @@ -0,0 +1,2 @@ +org.springframework.core.io.support.DummyFactory=org.springframework.core.io.support.MyDummyFactory2,org.springframework.core.io.support.MyDummyFactory1 +java.lang.String=org.springframework.core.io.support.MyDummyFactory1 \ No newline at end of file diff --git a/spring-core/src/test/resources/org/springframework/core/springFactoriesLoaderTests.properties b/spring-core/src/test/resources/org/springframework/core/springFactoriesLoaderTests.properties deleted file mode 100644 index a2813e29693..00000000000 --- a/spring-core/src/test/resources/org/springframework/core/springFactoriesLoaderTests.properties +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.core.DummyFactory=org.springframework.core.MyDummyFactory2,org.springframework.core.MyDummyFactory1 -java.lang.String=org.springframework.core.MyDummyFactory1 \ No newline at end of file