Skip to content

Commit 1a88007

Browse files
committed
Improve documentation of SpringFactoriesLoader
1 parent e1d7f08 commit 1a88007

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@
3636
/**
3737
* General purpose factory loading mechanism for internal use within the framework.
3838
*
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:
4345
*
4446
* <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
4547
*
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.
4850
*
4951
* @author Arjen Poutsma
5052
* @author Juergen Hoeller
@@ -53,18 +55,26 @@
5355
*/
5456
public abstract class SpringFactoriesLoader {
5557

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-
5958
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
6059

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+
6166

6267
/**
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.
6673
* @param factoryClass the interface or abstract class representing the factory
6774
* @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
6878
*/
6979
public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
7080
Assert.notNull(factoryClass, "'factoryClass' must not be null");
@@ -84,6 +94,16 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader class
8494
return result;
8595
}
8696

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+
*/
87107
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
88108
String factoryClassName = factoryClass.getName();
89109
try {

0 commit comments

Comments
 (0)