Top Description Inners Fields Constructors Methods
org.springframework.core.io.support

public Class SpringFactoriesLoader

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.aot.hint.support.SpringFactoriesLoaderRuntimeHints.ExtendedSpringFactoriesLoader
Imports
java.io.IOException, java.lang.reflect.Constructor, .Modifier, java.net.URL, java.util.ArrayList, .Arrays, .Collections, .Enumeration, .LinkedHashMap, .List, .Map, .Properties, java.util.function.BiConsumer, .BiFunction, .Function, .Supplier, kotlin.jvm.JvmClassMappingKt, kotlin.reflect.KFunction, kotlin.reflect.KParameter, kotlin.reflect.full.KClasses, kotlin.reflect.jvm.KCallablesJvm, kotlin.reflect.jvm.ReflectJvmMapping, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.KotlinDetector, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.core.io.UrlResource, org.springframework.core.log.LogMessage, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .CollectionUtils, .ConcurrentReferenceHashMap, .ReflectionUtils, .StringUtils

General purpose factory loading mechanism for internal use within the framework.

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.MyServiceImpl2
where 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:

If the resolvable constructor has arguments, a suitable ArgumentResolver should be provided. To customize how instantiation failures are handled, consider providing a FailureHandler.

Authors
Arjen Poutsma, Juergen Hoeller, Sam Brannen, Andy Wilkinson, Madhura Bhave, Phillip Webb
Since
3.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
SpringFactoriesLoader.ArgumentResolver

Strategy for resolving constructor arguments based on their type.

pack-priv static class
SpringFactoriesLoader.FactoryInstantiator<
the instance implementation type
T
>

Internal instantiator used to create the factory instance.

public static interface
SpringFactoriesLoader.FailureHandler

Strategy for handling a failure that occurs when instantiating a factory.

private static class
SpringFactoriesLoader.KotlinDelegate

Nested class to avoid a hard dependency on Kotlin at runtime.

Field Summary

Modifier and TypeField and Description
pack-priv static final Map<ClassLoader, Map<String, SpringFactoriesLoader>>
private final ClassLoader
private final Map<String, List<String>>
public static final String
FACTORIES_RESOURCE_LOCATION

The location to look for factories.

private static final Log
private static final SpringFactoriesLoader.FailureHandler

Constructor Summary

AccessConstructor and Description
protected
SpringFactoriesLoader(ClassLoader
the classloader used to instantiate the factories
classLoader
,
Map<String, List<String>>
a map of factory class name to implementation class names
factories
)

Create a new SpringFactoriesLoader instance.

Method Summary

Modifier and TypeMethod and Description
public static SpringFactoriesLoader

Returns:

a SpringFactoriesLoader instance
forDefaultResourceLocation
()

Create a SpringFactoriesLoader instance that will load and instantiate the factory implementations from META-INF/spring.factories, using the default class loader.

public static SpringFactoriesLoader

Returns:

a SpringFactoriesLoader instance
forDefaultResourceLocation
(ClassLoader
the ClassLoader to use for loading resources; can be null to use the default
classLoader
)

Create a SpringFactoriesLoader instance that will load and instantiate the factory implementations from META-INF/spring.factories, using the given class loader.

public static SpringFactoriesLoader

Returns:

a SpringFactoriesLoader instance
forResourceLocation
(String
the resource location to look for factories
resourceLocation
)

Create a SpringFactoriesLoader instance that will load and instantiate the factory implementations from the given location, using the default class loader.

public static SpringFactoriesLoader

Returns:

a SpringFactoriesLoader instance
forResourceLocation
(String
the resource location to look for factories
resourceLocation
,
ClassLoader
the ClassLoader to use for loading resources; can be null to use the default
classLoader
)

Create a SpringFactoriesLoader instance that will load and instantiate the factory implementations from the given location, using the given class loader.

protected <T> T
instantiateFactory(String implementationName, Class<T> type, SpringFactoriesLoader.ArgumentResolver argumentResolver, SpringFactoriesLoader.FailureHandler failureHandler)

public <T> List<T>
load(Class<T>
the interface or abstract class representing the factory
factoryType
)

Load and instantiate the factory implementations of the given type from META-INF/spring.factories, using the configured class loader and a default argument resolver that expects a no-arg constructor.

public <T> List<T>
load(Class<T>
the interface or abstract class representing the factory
factoryType
,
SpringFactoriesLoader.ArgumentResolver
strategy used to resolve constructor arguments by their type
argumentResolver
)

Load and instantiate the factory implementations of the given type from META-INF/spring.factories, using the configured class loader and the given argument resolver.

public <T> List<T>
load(Class<T>
the interface or abstract class representing the factory
factoryType
,
SpringFactoriesLoader.FailureHandler
strategy used to handle factory instantiation failures
failureHandler
)

Load and instantiate the factory implementations of the given type from META-INF/spring.factories, using the configured class loader with custom failure handling provided by the given failure handler.

public <T> List<T>
load(Class<T>
the interface or abstract class representing the factory
factoryType
,
SpringFactoriesLoader.ArgumentResolver
strategy used to resolve constructor arguments by their type
argumentResolver
,
SpringFactoriesLoader.FailureHandler
strategy used to handle factory instantiation failures
failureHandler
)

Load and instantiate the factory implementations of the given type from META-INF/spring.factories, using the configured class loader, the given argument resolver, and custom failure handling provided by the given failure handler.

public static <T> List<T>
loadFactories(Class<T>
the interface or abstract class representing the factory
factoryType
,
ClassLoader
the ClassLoader to use for loading (can be null to use the default)
classLoader
)

Load and instantiate the factory implementations of the given type from META-INF/spring.factories, using the given class loader.

protected static Map<String, List<String>>
loadFactoriesResource(ClassLoader classLoader, String resourceLocation)

private List<String>
loadFactoryNames(Class<?> factoryType)

public static List<String>
loadFactoryNames(Class<?>
the interface or abstract class representing the factory
factoryType
,
ClassLoader
the ClassLoader to use for loading resources; can be null to use the default
classLoader
)
Deprecated since 6.0. as of 6.0 in favor of load(Class, ArgumentResolver, FailureHandler)

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 List<String>
toDistinctUnmodifiableList(String factoryType, List<String> implementations)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait