@@ -114,43 +114,17 @@ public class SpringFactoriesLoader {
114
114
private final Map <String , List <String >> factories ;
115
115
116
116
117
- private SpringFactoriesLoader (@ Nullable ClassLoader classLoader , String resourceLocation ) {
118
- this .classLoader = classLoader ;
119
- this .factories = loadFactoriesResource ((classLoader != null ) ? classLoader
120
- : SpringFactoriesLoader .class .getClassLoader (), resourceLocation );
121
- }
122
-
117
+ /**
118
+ * Create a new {@link SpringFactoriesLoader} instance.
119
+ * @param classLoader the classloader used to instantiate the factories
120
+ * @param factories a map of factory class name to implementation class names
121
+ */
123
122
protected SpringFactoriesLoader (@ Nullable ClassLoader classLoader , Map <String , List <String >> factories ) {
124
123
this .classLoader = classLoader ;
125
124
this .factories = factories ;
126
125
}
127
126
128
127
129
- private Map <String , List <String >> loadFactoriesResource (ClassLoader classLoader , String resourceLocation ) {
130
- Map <String , List <String >> result = new LinkedHashMap <>();
131
- try {
132
- Enumeration <URL > urls = classLoader .getResources (resourceLocation );
133
- while (urls .hasMoreElements ()) {
134
- UrlResource resource = new UrlResource (urls .nextElement ());
135
- Properties properties = PropertiesLoaderUtils .loadProperties (resource );
136
- properties .forEach ((name , value ) -> {
137
- List <String > implementations = result .computeIfAbsent (((String ) name ).trim (), key -> new ArrayList <>());
138
- Arrays .stream (StringUtils .commaDelimitedListToStringArray ((String ) value ))
139
- .map (String ::trim ).forEach (implementations ::add );
140
- });
141
- }
142
- result .replaceAll (this ::toDistinctUnmodifiableList );
143
- }
144
- catch (IOException ex ) {
145
- throw new IllegalArgumentException ("Unable to load factories from location [" + resourceLocation + "]" , ex );
146
- }
147
- return Collections .unmodifiableMap (result );
148
- }
149
-
150
- private List <String > toDistinctUnmodifiableList (String factoryType , List <String > implementations ) {
151
- return implementations .stream ().distinct ().toList ();
152
- }
153
-
154
128
/**
155
129
* Load and instantiate the factory implementations of the given type from
156
130
* {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader and
@@ -343,19 +317,47 @@ public static SpringFactoriesLoader forResourceLocation(String resourceLocation)
343
317
*/
344
318
public static SpringFactoriesLoader forResourceLocation (@ Nullable ClassLoader classLoader , String resourceLocation ) {
345
319
Assert .hasText (resourceLocation , "'resourceLocation' must not be empty" );
346
- Map <String , SpringFactoriesLoader > loaders = SpringFactoriesLoader .cache .get (classLoader );
320
+ ClassLoader resourceClassLoader = (classLoader != null ) ? classLoader
321
+ : SpringFactoriesLoader .class .getClassLoader ();
322
+ Map <String , SpringFactoriesLoader > loaders = SpringFactoriesLoader .cache .get (resourceClassLoader );
347
323
if (loaders == null ) {
348
324
loaders = new ConcurrentReferenceHashMap <>();
349
- SpringFactoriesLoader .cache .put (classLoader , loaders );
325
+ SpringFactoriesLoader .cache .put (resourceClassLoader , loaders );
350
326
}
351
327
SpringFactoriesLoader loader = loaders .get (resourceLocation );
352
328
if (loader == null ) {
353
- loader = new SpringFactoriesLoader (classLoader , resourceLocation );
329
+ Map <String , List <String >> factories = loadFactoriesResource (resourceClassLoader , resourceLocation );
330
+ loader = new SpringFactoriesLoader (classLoader , factories );
354
331
loaders .put (resourceLocation , loader );
355
332
}
356
333
return loader ;
357
334
}
358
335
336
+ private static Map <String , List <String >> loadFactoriesResource (ClassLoader classLoader , String resourceLocation ) {
337
+ Map <String , List <String >> result = new LinkedHashMap <>();
338
+ try {
339
+ Enumeration <URL > urls = classLoader .getResources (resourceLocation );
340
+ while (urls .hasMoreElements ()) {
341
+ UrlResource resource = new UrlResource (urls .nextElement ());
342
+ Properties properties = PropertiesLoaderUtils .loadProperties (resource );
343
+ properties .forEach ((name , value ) -> {
344
+ List <String > implementations = result .computeIfAbsent (((String ) name ).trim (), key -> new ArrayList <>());
345
+ Arrays .stream (StringUtils .commaDelimitedListToStringArray ((String ) value ))
346
+ .map (String ::trim ).forEach (implementations ::add );
347
+ });
348
+ }
349
+ result .replaceAll (SpringFactoriesLoader ::toDistinctUnmodifiableList );
350
+ }
351
+ catch (IOException ex ) {
352
+ throw new IllegalArgumentException ("Unable to load factories from location [" + resourceLocation + "]" , ex );
353
+ }
354
+ return Collections .unmodifiableMap (result );
355
+ }
356
+
357
+ private static List <String > toDistinctUnmodifiableList (String factoryType , List <String > implementations ) {
358
+ return implementations .stream ().distinct ().toList ();
359
+ }
360
+
359
361
360
362
/**
361
363
* Internal instantiator used to create the factory instance.
0 commit comments