36
36
/**
37
37
* General purpose factory loading mechanism for internal use within the framework.
38
38
*
39
- * <p>The {@code SpringFactoriesLoader} loads and instantiates factories of a given type
40
- * from "META-INF/spring.factories" files. The file should be in {@link Properties} format,
41
- * where the key is the fully qualified interface or abstract class name, and the value
42
- * is a comma-separated list of implementation class names. For instance:
39
+ * <p>{@code SpringFactoriesLoader} {@linkplain #loadFactories loads} and instantiates
40
+ * factories of a given type from {@value #FACTORIES_RESOURCE_LOCATION} files which
41
+ * may be present in multiple JAR files in the classpath. The {@code spring.factories}
42
+ * file must be in {@link Properties} format, where the key is the fully qualified
43
+ * name of the interface or abstract class, and the value is a comma-separated list of
44
+ * implementation class names. For example:
43
45
*
44
46
* <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
45
47
*
46
- * where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
47
- * {@code MyServiceImpl2} are the two implementations.
48
+ * where {@code example. MyService} is the name of the interface, and {@code MyServiceImpl1}
49
+ * and {@code MyServiceImpl2} are two implementations.
48
50
*
49
51
* @author Arjen Poutsma
50
52
* @author Juergen Hoeller
53
55
*/
54
56
public abstract class SpringFactoriesLoader {
55
57
56
- /** The location to look for the factories. Can be present in multiple JAR files. */
57
- public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories" ;
58
-
59
58
private static final Log logger = LogFactory .getLog (SpringFactoriesLoader .class );
60
59
60
+ /**
61
+ * The location to look for factories.
62
+ * <p>Can be present in multiple JAR files.
63
+ */
64
+ public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories" ;
65
+
61
66
62
67
/**
63
- * Load the factory implementations of the given type from the default location,
64
- * using the given class loader.
65
- * <p>The returned factories are ordered in accordance with the {@link AnnotationAwareOrderComparator}.
68
+ * Load and instantiate the factory implementations of the given type from
69
+ * {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader.
70
+ * <p>The returned factories are sorted in accordance with the {@link AnnotationAwareOrderComparator}.
71
+ * <p>If a custom instantiation strategy is required, use {@link #loadFactoryNames}
72
+ * to obtain all registered factory names.
66
73
* @param factoryClass the interface or abstract class representing the factory
67
74
* @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
75
+ * @see #loadFactoryNames
76
+ * @throws IllegalArgumentException if any factory implementation class cannot
77
+ * be loaded or if an error occurs while instantiating any factory
68
78
*/
69
79
public static <T > List <T > loadFactories (Class <T > factoryClass , ClassLoader classLoader ) {
70
80
Assert .notNull (factoryClass , "'factoryClass' must not be null" );
@@ -84,6 +94,16 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader class
84
94
return result ;
85
95
}
86
96
97
+ /**
98
+ * Load the fully qualified class names of factory implementations of the
99
+ * given type from {@value #FACTORIES_RESOURCE_LOCATION}, using the given
100
+ * class loader.
101
+ * @param factoryClass the interface or abstract class representing the factory
102
+ * @param classLoader the ClassLoader to use for loading resources; can be
103
+ * {@code null} to use the default
104
+ * @see #loadFactories
105
+ * @throws IllegalArgumentException if an error occurs while loading factory names
106
+ */
87
107
public static List <String > loadFactoryNames (Class <?> factoryClass , ClassLoader classLoader ) {
88
108
String factoryClassName = factoryClass .getName ();
89
109
try {
0 commit comments