Moved SpringFactoriesLoader to io.support in order to resolve tangle.

This commit is contained in:
Arjen Poutsma 2012-09-10 11:28:45 +02:00
parent fc859ffd6e
commit aeff91c1da
7 changed files with 54 additions and 42 deletions

View File

@ -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;
* <p>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:
* <pre class="code">
* example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
* </pre>
*
* <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
*
* 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.
*
* <p>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 <T> List<T> loadFactories(Class<T> factoryClass) {
return loadFactories(factoryClass, null, null);
}
/**
* Loads the factory implementations of the given type from the default location, using
* the given class loader.
*
* <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
* <p>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 <T> List<T> loadFactories(Class<T> 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.
*
* <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
* <p>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 <T> List<T> loadFactories(Class<T> 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<String> loadFactoryNames(Class<?> factoryClass,
ClassLoader classLoader,
String factoriesLocation) {
ClassLoader classLoader, String factoriesLocation) {
String factoryClassName = factoryClass.getName();
@ -129,7 +142,7 @@ public abstract class SpringFactoriesLoader {
List<String> result = new ArrayList<String>();
Enumeration<URL> 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> T instantiateFactory(String instanceClassName,
Class<T> factoryClass,
ClassLoader classLoader) {
Class<T> factoryClass, ClassLoader classLoader) {
try {
Class<?> instanceClass = ClassUtils.forName(instanceClassName, classLoader);
if (!factoryClass.isAssignableFrom(instanceClass)) {

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.core;
package org.springframework.core.io.support;
/**
* Used by {@link SpringFactoriesLoaderTests}

View File

@ -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";

View File

@ -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";

View File

@ -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<DummyFactory> 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);
}
}

View File

@ -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

View File

@ -1,2 +0,0 @@
org.springframework.core.DummyFactory=org.springframework.core.MyDummyFactory2,org.springframework.core.MyDummyFactory1
java.lang.String=org.springframework.core.MyDummyFactory1