Class SpringFactoriesLoader
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.
Implementation classes must have a single resolvable constructor that will be used to create the instance, either:
- a primary or single constructor
- a single public constructor
- the default constructor
ArgumentResolver
should be provided. To customize how instantiation failures
are handled, consider providing a FailureHandler
.- Since:
- 3.2
- Author:
- Arjen Poutsma, Juergen Hoeller, Sam Brannen, Andy Wilkinson, Madhura Bhave, Phillip Webb
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Strategy for resolving constructor arguments based on their type.static interface
Strategy for handling a failure that occurs when instantiating a factory. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T>
loadFactories
(Class<T> factoryType, ClassLoader classLoader) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader and a default argument resolver that expects a no-arg constructor.static <T> List<T>
loadFactories
(Class<T> factoryType, ClassLoader classLoader, SpringFactoriesLoader.ArgumentResolver argumentResolver) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader and argument resolver.static <T> List<T>
loadFactories
(Class<T> factoryType, ClassLoader classLoader, SpringFactoriesLoader.ArgumentResolver argumentResolver, SpringFactoriesLoader.FailureHandler failureHandler) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader, argument resolver, and custom failure handling provided by the given failure handler.static <T> List<T>
loadFactories
(Class<T> factoryType, ClassLoader classLoader, SpringFactoriesLoader.FailureHandler failureHandler) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader with custom failure handling provided by the given failure handler.loadFactoryNames
(Class<?> factoryType, 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.
-
Field Details
-
FACTORIES_RESOURCE_LOCATION
The location to look for factories.Can be present in multiple JAR files.
- See Also:
-
-
Method Details
-
loadFactories
Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader and a default argument resolver that expects a no-arg constructor.The returned factories are sorted through
AnnotationAwareOrderComparator
.If a custom instantiation strategy is required, use
loadFactories
with a customArgumentResolver
and/orFailureHandler
.As of Spring Framework 5.3, if duplicate implementation class names are discovered for a given factory type, only one instance of the duplicated implementation type will be instantiated.
- Parameters:
factoryType
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading (can benull
to use the default)- Throws:
IllegalArgumentException
- if any factory implementation class cannot be loaded or if an error occurs while instantiating any factory
-
loadFactories
public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader, @Nullable SpringFactoriesLoader.ArgumentResolver argumentResolver) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader and argument resolver.The returned factories are sorted through
AnnotationAwareOrderComparator
.As of Spring Framework 5.3, if duplicate implementation class names are discovered for a given factory type, only one instance of the duplicated implementation type will be instantiated.
- Parameters:
factoryType
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading (can benull
to use the default)argumentResolver
- strategy used to resolve constructor arguments by their type- Throws:
IllegalArgumentException
- if any factory implementation class cannot be loaded or if an error occurs while instantiating any factory- Since:
- 6.0
-
loadFactories
public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader, @Nullable SpringFactoriesLoader.FailureHandler failureHandler) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader with custom failure handling provided by the given failure handler.The returned factories are sorted through
AnnotationAwareOrderComparator
.As of Spring Framework 5.3, if duplicate implementation class names are discovered for a given factory type, only one instance of the duplicated implementation type will be instantiated.
For any factory implementation class that cannot be loaded or error that occurs while instantiating it, the given failure handler is called.
- Parameters:
factoryType
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading (can benull
to use the default)failureHandler
- strategy used to handle factory instantiation failures- Since:
- 6.0
-
loadFactories
public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader, @Nullable SpringFactoriesLoader.ArgumentResolver argumentResolver, @Nullable SpringFactoriesLoader.FailureHandler failureHandler) Load and instantiate the factory implementations of the given type from "META-INF/spring.factories", using the given class loader, argument resolver, and custom failure handling provided by the given failure handler.The returned factories are sorted through
AnnotationAwareOrderComparator
.As of Spring Framework 5.3, if duplicate implementation class names are discovered for a given factory type, only one instance of the duplicated implementation type will be instantiated.
For any factory implementation class that cannot be loaded or error that occurs while instantiating it, the given failure handler is called.
- Parameters:
factoryType
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading (can benull
to use the default)argumentResolver
- strategy used to resolve constructor arguments by their typefailureHandler
- strategy used to handle factory instantiation failures- Since:
- 6.0
-
loadFactoryNames
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable 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.As of Spring Framework 5.3, if a particular implementation class name is discovered more than once for the given factory type, duplicates will be ignored.
- Parameters:
factoryType
- the interface or abstract class representing the factoryclassLoader
- the ClassLoader to use for loading resources; can benull
to use the default- Throws:
IllegalArgumentException
- if an error occurs while loading factory names- See Also:
-