public abstract class SpringFactoriesLoader
extends java.lang.Object
SpringFactoriesLoader
loads and instantiates
factories of a given type from "META-INF/spring.factories" files which
may be present in multiple JAR files in the classpath. The spring.factories
file must be in Properties
format, where the key is the fully qualified
name of the interface or abstract class, and the value is a comma-separated list of
implementation class names. For example:
example.MyService=example.MyServiceImpl1,example.MyServiceImpl2where
example.MyService
is the name of the interface, and MyServiceImpl1
and MyServiceImpl2
are two implementations.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
FACTORIES_RESOURCE_LOCATION
The location to look for factories.
|
private static Log |
logger |
Constructor and Description |
---|
SpringFactoriesLoader() |
Modifier and Type | Method and Description |
---|---|
private static <T> T |
instantiateFactory(java.lang.String instanceClassName,
java.lang.Class<T> factoryClass,
java.lang.ClassLoader classLoader) |
static <T> java.util.List<T> |
loadFactories(java.lang.Class<T> factoryClass,
java.lang.ClassLoader classLoader)
Load and instantiate the factory implementations of the given type from
"META-INF/spring.factories", using the given class loader.
|
static java.util.List<java.lang.String> |
loadFactoryNames(java.lang.Class<?> factoryClass,
java.lang.ClassLoader classLoader)
Load the fully qualified class names of factory implementations of the
given type from "META-INF/spring.factories", using the given
class loader.
|
private static final Log logger
public static final java.lang.String FACTORIES_RESOURCE_LOCATION
Can be present in multiple JAR files.
public static <T> java.util.List<T> loadFactories(java.lang.Class<T> factoryClass, java.lang.ClassLoader classLoader)
The returned factories are sorted in accordance with the AnnotationAwareOrderComparator
.
If a custom instantiation strategy is required, use loadFactoryNames(java.lang.Class<?>, java.lang.ClassLoader)
to obtain all registered factory names.
factoryClass
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading (can be null
to use the default)java.lang.IllegalArgumentException
- if any factory implementation class cannot
be loaded or if an error occurs while instantiating any factoryloadFactoryNames(java.lang.Class<?>, java.lang.ClassLoader)
public static java.util.List<java.lang.String> loadFactoryNames(java.lang.Class<?> factoryClass, java.lang.ClassLoader classLoader)
factoryClass
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading resources; can be
null
to use the defaultjava.lang.IllegalArgumentException
- if an error occurs while loading factory namesloadFactories(java.lang.Class<T>, java.lang.ClassLoader)
private static <T> T instantiateFactory(java.lang.String instanceClassName, java.lang.Class<T> factoryClass, java.lang.ClassLoader classLoader)