14
14
* limitations under the License.
15
15
*/
16
16
17
- package org .springframework .core ;
17
+ package org .springframework .core . io . support ;
18
18
19
19
import java .io .IOException ;
20
20
import java .net .URL ;
28
28
import org .apache .commons .logging .Log ;
29
29
import org .apache .commons .logging .LogFactory ;
30
30
31
+ import org .springframework .core .annotation .AnnotationAwareOrderComparator ;
31
32
import org .springframework .core .io .UrlResource ;
32
- import org .springframework .core .io .support .PropertiesLoaderUtils ;
33
33
import org .springframework .util .Assert ;
34
34
import org .springframework .util .ClassUtils ;
35
35
import org .springframework .util .StringUtils ;
44
44
* <p>The file should be in {@link Properties} format, where the key is the fully
45
45
* qualified interface or abstract class name, and the value is a comma separated list of
46
46
* implementations. For instance:
47
- * <pre class="code">
48
- * example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
49
- * </pre>
47
+ *
48
+ * <pre class="code"> example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
49
+ *
50
50
* where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
51
51
* {@code MyServiceImpl2} are the two implementations.
52
52
*
@@ -60,38 +60,52 @@ public abstract class SpringFactoriesLoader {
60
60
/** The location to look for the factories. Can be present in multiple JAR files. */
61
61
public static final String DEFAULT_FACTORIES_LOCATION = "META-INF/spring.factories" ;
62
62
63
+ /**
64
+ * Loads the factory implementations of the given type from the default location.
65
+ *
66
+ * <p>The returned factories are ordered in accordance with the {@link
67
+ * AnnotationAwareOrderComparator}.
68
+ *
69
+ * @param factoryClass the interface or abstract class representing the factory
70
+ * @throws IllegalArgumentException in case of errors
71
+ */
72
+ public static <T > List <T > loadFactories (Class <T > factoryClass ) {
73
+ return loadFactories (factoryClass , null , null );
74
+ }
75
+
63
76
/**
64
77
* Loads the factory implementations of the given type from the default location, using
65
78
* the given class loader.
66
79
*
67
- * <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
80
+ * <p>The returned factories are ordered in accordance with the {@link
81
+ * AnnotationAwareOrderComparator}.
68
82
*
69
83
* @param factoryClass the interface or abstract class representing the factory
70
- * @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
71
- * default
84
+ * @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
85
+ * default
72
86
* @throws IllegalArgumentException in case of errors
73
87
*/
74
88
public static <T > List <T > loadFactories (Class <T > factoryClass ,
75
- ClassLoader classLoader ) {
89
+ ClassLoader classLoader ) {
76
90
return loadFactories (factoryClass , classLoader , null );
77
91
}
78
92
79
93
/**
80
94
* Loads the factory implementations of the given type from the given location, using the
81
95
* given class loader.
82
96
*
83
- * <p>The returned factories are ordered in accordance with the {@link OrderComparator}.
97
+ * <p>The returned factories are ordered in accordance with the {@link
98
+ * AnnotationAwareOrderComparator}.
84
99
*
85
- * @param factoryClass the interface or abstract class representing the factory
86
- * @param classLoader the ClassLoader to use for loading, can be {@code null} to
87
- * use the default
100
+ * @param factoryClass the interface or abstract class representing the factory
101
+ * @param classLoader the ClassLoader to use for loading, can be {@code null} to use the
102
+ * default
88
103
* @param factoriesLocation the factories file location, can be {@code null} to use the
89
- * {@linkplain #DEFAULT_FACTORIES_LOCATION default}
104
+ * {@linkplain #DEFAULT_FACTORIES_LOCATION default}
90
105
* @throws IllegalArgumentException in case of errors
91
106
*/
92
107
public static <T > List <T > loadFactories (Class <T > factoryClass ,
93
- ClassLoader classLoader ,
94
- String factoriesLocation ) {
108
+ ClassLoader classLoader , String factoriesLocation ) {
95
109
Assert .notNull (factoryClass , "'factoryClass' must not be null" );
96
110
97
111
if (classLoader == null ) {
@@ -114,22 +128,21 @@ public static <T> List<T> loadFactories(Class<T> factoryClass,
114
128
result .add (instantiateFactory (factoryName , factoryClass , classLoader ));
115
129
}
116
130
117
- Collections .sort (result , new OrderComparator ());
131
+ Collections .sort (result , new AnnotationAwareOrderComparator ());
118
132
119
133
return result ;
120
134
}
121
135
122
136
private static List <String > loadFactoryNames (Class <?> factoryClass ,
123
- ClassLoader classLoader ,
124
- String factoriesLocation ) {
137
+ ClassLoader classLoader , String factoriesLocation ) {
125
138
126
139
String factoryClassName = factoryClass .getName ();
127
140
128
141
try {
129
142
List <String > result = new ArrayList <String >();
130
143
Enumeration <URL > urls = classLoader .getResources (factoriesLocation );
131
144
while (urls .hasMoreElements ()) {
132
- URL url = ( URL ) urls .nextElement ();
145
+ URL url = urls .nextElement ();
133
146
Properties properties =
134
147
PropertiesLoaderUtils .loadProperties (new UrlResource (url ));
135
148
String factoryClassNames = properties .getProperty (factoryClassName );
@@ -150,8 +163,7 @@ private static List<String> loadFactoryNames(Class<?> factoryClass,
150
163
151
164
@ SuppressWarnings ("unchecked" )
152
165
private static <T > T instantiateFactory (String instanceClassName ,
153
- Class <T > factoryClass ,
154
- ClassLoader classLoader ) {
166
+ Class <T > factoryClass , ClassLoader classLoader ) {
155
167
try {
156
168
Class <?> instanceClass = ClassUtils .forName (instanceClassName , classLoader );
157
169
if (!factoryClass .isAssignableFrom (instanceClass )) {
0 commit comments