Moved SpringFactoriesLoader to io.support in order to resolve tangle.
This commit is contained in:
parent
fc859ffd6e
commit
aeff91c1da
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.core;
|
package org.springframework.core.io.support;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
@ -28,8 +28,8 @@ import java.util.Properties;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.StringUtils;
|
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
|
* <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
|
* qualified interface or abstract class name, and the value is a comma separated list of
|
||||||
* implementations. For instance:
|
* implementations. For instance:
|
||||||
* <pre class="code">
|
*
|
||||||
* example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
|
* <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
|
||||||
* </pre>
|
*
|
||||||
* where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
|
* where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
|
||||||
* {@code MyServiceImpl2} are the two implementations.
|
* {@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. */
|
/** 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";
|
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
|
* Loads the factory implementations of the given type from the default location, using
|
||||||
* the given class loader.
|
* 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 factoryClass the interface or abstract class representing the factory
|
||||||
* @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
|
* @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
|
||||||
* default
|
* default
|
||||||
* @throws IllegalArgumentException in case of errors
|
* @throws IllegalArgumentException in case of errors
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> loadFactories(Class<T> factoryClass,
|
public static <T> List<T> loadFactories(Class<T> factoryClass,
|
||||||
ClassLoader classLoader) {
|
ClassLoader classLoader) {
|
||||||
return loadFactories(factoryClass, classLoader, null);
|
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
|
* Loads the factory implementations of the given type from the given location, using the
|
||||||
* given class loader.
|
* 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 factoryClass the interface or abstract class representing the factory
|
||||||
* @param classLoader the ClassLoader to use for loading, can be {@code null} to
|
* @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
|
||||||
* use the default
|
* default
|
||||||
* @param factoriesLocation the factories file location, can be {@code null} to use the
|
* @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
|
* @throws IllegalArgumentException in case of errors
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> loadFactories(Class<T> factoryClass,
|
public static <T> List<T> loadFactories(Class<T> factoryClass,
|
||||||
ClassLoader classLoader,
|
ClassLoader classLoader, String factoriesLocation) {
|
||||||
String factoriesLocation) {
|
|
||||||
Assert.notNull(factoryClass, "'factoryClass' must not be null");
|
Assert.notNull(factoryClass, "'factoryClass' must not be null");
|
||||||
|
|
||||||
if (classLoader == null) {
|
if (classLoader == null) {
|
||||||
|
|
@ -114,14 +128,13 @@ public abstract class SpringFactoriesLoader {
|
||||||
result.add(instantiateFactory(factoryName, factoryClass, classLoader));
|
result.add(instantiateFactory(factoryName, factoryClass, classLoader));
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.sort(result, new OrderComparator());
|
Collections.sort(result, new AnnotationAwareOrderComparator());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> loadFactoryNames(Class<?> factoryClass,
|
private static List<String> loadFactoryNames(Class<?> factoryClass,
|
||||||
ClassLoader classLoader,
|
ClassLoader classLoader, String factoriesLocation) {
|
||||||
String factoriesLocation) {
|
|
||||||
|
|
||||||
String factoryClassName = factoryClass.getName();
|
String factoryClassName = factoryClass.getName();
|
||||||
|
|
||||||
|
|
@ -129,7 +142,7 @@ public abstract class SpringFactoriesLoader {
|
||||||
List<String> result = new ArrayList<String>();
|
List<String> result = new ArrayList<String>();
|
||||||
Enumeration<URL> urls = classLoader.getResources(factoriesLocation);
|
Enumeration<URL> urls = classLoader.getResources(factoriesLocation);
|
||||||
while (urls.hasMoreElements()) {
|
while (urls.hasMoreElements()) {
|
||||||
URL url = (URL) urls.nextElement();
|
URL url = urls.nextElement();
|
||||||
Properties properties =
|
Properties properties =
|
||||||
PropertiesLoaderUtils.loadProperties(new UrlResource(url));
|
PropertiesLoaderUtils.loadProperties(new UrlResource(url));
|
||||||
String factoryClassNames = properties.getProperty(factoryClassName);
|
String factoryClassNames = properties.getProperty(factoryClassName);
|
||||||
|
|
@ -150,8 +163,7 @@ public abstract class SpringFactoriesLoader {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static <T> T instantiateFactory(String instanceClassName,
|
private static <T> T instantiateFactory(String instanceClassName,
|
||||||
Class<T> factoryClass,
|
Class<T> factoryClass, ClassLoader classLoader) {
|
||||||
ClassLoader classLoader) {
|
|
||||||
try {
|
try {
|
||||||
Class<?> instanceClass = ClassUtils.forName(instanceClassName, classLoader);
|
Class<?> instanceClass = ClassUtils.forName(instanceClassName, classLoader);
|
||||||
if (!factoryClass.isAssignableFrom(instanceClass)) {
|
if (!factoryClass.isAssignableFrom(instanceClass)) {
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.core;
|
package org.springframework.core.io.support;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by {@link SpringFactoriesLoaderTests}
|
* Used by {@link SpringFactoriesLoaderTests}
|
||||||
|
|
@ -14,18 +14,17 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.core;
|
package org.springframework.core.io.support;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by {@link SpringFactoriesLoaderTests}
|
* Used by {@link SpringFactoriesLoaderTests}
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
*/
|
*/
|
||||||
public class MyDummyFactory1 implements DummyFactory, Ordered {
|
@Order(1)
|
||||||
|
public class MyDummyFactory1 implements DummyFactory {
|
||||||
public int getOrder() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getString() {
|
public String getString() {
|
||||||
return "Foo";
|
return "Foo";
|
||||||
|
|
@ -14,18 +14,17 @@
|
||||||
* limitations under the License.
|
* 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
|
* @author Arjen Poutsma
|
||||||
*/
|
*/
|
||||||
public class MyDummyFactory2 implements DummyFactory, Ordered {
|
@Order(2)
|
||||||
|
public class MyDummyFactory2 implements DummyFactory {
|
||||||
public int getOrder() {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getString() {
|
public String getString() {
|
||||||
return "Bar";
|
return "Bar";
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.core;
|
package org.springframework.core.io.support;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -25,11 +25,13 @@ import org.junit.Test;
|
||||||
/** @author Arjen Poutsma */
|
/** @author Arjen Poutsma */
|
||||||
public class SpringFactoriesLoaderTests {
|
public class SpringFactoriesLoaderTests {
|
||||||
|
|
||||||
|
private static final String FACTORIES_LOCATION =
|
||||||
|
"org/springframework/core/io/support/springFactoriesLoaderTests.properties";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadFactories() {
|
public void loadFactories() {
|
||||||
List<DummyFactory> factories = SpringFactoriesLoader
|
List<DummyFactory> factories = SpringFactoriesLoader
|
||||||
.loadFactories(DummyFactory.class, null,
|
.loadFactories(DummyFactory.class, null, FACTORIES_LOCATION);
|
||||||
"org/springframework/core/springFactoriesLoaderTests.properties");
|
|
||||||
|
|
||||||
assertEquals(2, factories.size());
|
assertEquals(2, factories.size());
|
||||||
assertTrue(factories.get(0) instanceof MyDummyFactory1);
|
assertTrue(factories.get(0) instanceof MyDummyFactory1);
|
||||||
|
|
@ -39,7 +41,7 @@ public class SpringFactoriesLoaderTests {
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void loadInvalid() {
|
public void loadInvalid() {
|
||||||
SpringFactoriesLoader.loadFactories(String.class, null,
|
SpringFactoriesLoader.loadFactories(String.class, null,
|
||||||
"org/springframework/core/springFactoriesLoaderTests.properties");
|
FACTORIES_LOCATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
org.springframework.core.DummyFactory=org.springframework.core.MyDummyFactory2,org.springframework.core.MyDummyFactory1
|
|
||||||
java.lang.String=org.springframework.core.MyDummyFactory1
|
|
||||||
Loading…
Reference in New Issue