@@ -118,6 +118,7 @@ public class SpringFactoriesLoader {
118
118
* Create a new {@link SpringFactoriesLoader} instance.
119
119
* @param classLoader the classloader used to instantiate the factories
120
120
* @param factories a map of factory class name to implementation class names
121
+ * @since 6.0
121
122
*/
122
123
protected SpringFactoriesLoader (@ Nullable ClassLoader classLoader , Map <String , List <String >> factories ) {
123
124
this .classLoader = classLoader ;
@@ -127,10 +128,10 @@ protected SpringFactoriesLoader(@Nullable ClassLoader classLoader, Map<String, L
127
128
128
129
/**
129
130
* Load and instantiate the factory implementations of the given type from
130
- * {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader and
131
- * a default argument resolver that expects a no-arg constructor.
131
+ * {@value #FACTORIES_RESOURCE_LOCATION}, using the configured class loader
132
+ * and a default argument resolver that expects a no-arg constructor.
132
133
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
133
- * <p>If a custom instantiation strategy is required, use {@code loadFactories }
134
+ * <p>If a custom instantiation strategy is required, use {@code load(...) }
134
135
* with a custom {@link ArgumentResolver ArgumentResolver} and/or
135
136
* {@link FailureHandler FailureHandler}.
136
137
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
@@ -139,15 +140,16 @@ protected SpringFactoriesLoader(@Nullable ClassLoader classLoader, Map<String, L
139
140
* @param factoryType the interface or abstract class representing the factory
140
141
* @throws IllegalArgumentException if any factory implementation class cannot
141
142
* be loaded or if an error occurs while instantiating any factory
143
+ * @since 6.0
142
144
*/
143
145
public <T > List <T > load (Class <T > factoryType ) {
144
146
return load (factoryType , NO_ARGUMENT_RESOLVER , NO_FAILURE_HANDLER );
145
147
}
146
148
147
149
/**
148
150
* Load and instantiate the factory implementations of the given type from
149
- * {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader and
150
- * argument resolver.
151
+ * {@value #FACTORIES_RESOURCE_LOCATION}, using the configured class loader
152
+ * and the given argument resolver.
151
153
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
152
154
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
153
155
* discovered for a given factory type, only one instance of the duplicated
@@ -164,14 +166,14 @@ public <T> List<T> load(Class<T> factoryType, @Nullable ArgumentResolver argumen
164
166
165
167
/**
166
168
* Load and instantiate the factory implementations of the given type from
167
- * {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader with
168
- * custom failure handling provided by the given failure handler.
169
+ * {@value #FACTORIES_RESOURCE_LOCATION}, using the configured class loader
170
+ * with custom failure handling provided by the given failure handler.
169
171
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
170
172
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
171
173
* discovered for a given factory type, only one instance of the duplicated
172
174
* implementation type will be instantiated.
173
- * <p>For any factory implementation class that cannot be loaded or error that occurs while
174
- * instantiating it, the given failure handler is called.
175
+ * <p>For any factory implementation class that cannot be loaded or error that
176
+ * occurs while instantiating it, the given failure handler is called.
175
177
* @param factoryType the interface or abstract class representing the factory
176
178
* @param failureHandler strategy used to handle factory instantiation failures
177
179
* @since 6.0
@@ -182,15 +184,15 @@ public <T> List<T> load(Class<T> factoryType, @Nullable FailureHandler failureHa
182
184
183
185
/**
184
186
* Load and instantiate the factory implementations of the given type from
185
- * {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader,
186
- * argument resolver, and custom failure handling provided by the given
187
+ * {@value #FACTORIES_RESOURCE_LOCATION}, using the configured class loader,
188
+ * the given argument resolver, and custom failure handling provided by the given
187
189
* failure handler.
188
190
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
189
191
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
190
192
* discovered for a given factory type, only one instance of the duplicated
191
193
* implementation type will be instantiated.
192
- * <p>For any factory implementation class that cannot be loaded or error that occurs while
193
- * instantiating it, the given failure handler is called.
194
+ * <p>For any factory implementation class that cannot be loaded or error that
195
+ * occurs while instantiating it, the given failure handler is called.
194
196
* @param factoryType the interface or abstract class representing the factory
195
197
* @param argumentResolver strategy used to resolve constructor arguments by their type
196
198
* @param failureHandler strategy used to handle factory instantiation failures
@@ -233,6 +235,7 @@ protected <T> T instantiateFactory(String implementationName, Class<T> type,
233
235
}
234
236
}
235
237
238
+
236
239
/**
237
240
* Load and instantiate the factory implementations of the given type from
238
241
* {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader.
@@ -244,7 +247,8 @@ protected <T> T instantiateFactory(String implementationName, Class<T> type,
244
247
* {@link FailureHandler} support use {@link #forDefaultResourceLocation(ClassLoader)}
245
248
* to obtain a {@link SpringFactoriesLoader} instance.
246
249
* @param factoryType the interface or abstract class representing the factory
247
- * @param classLoader the ClassLoader to use for loading (can be {@code null} to use the default)
250
+ * @param classLoader the ClassLoader to use for loading (can be {@code null}
251
+ * to use the default)
248
252
* @throws IllegalArgumentException if any factory implementation class cannot
249
253
* be loaded or if an error occurs while instantiating any factory
250
254
*/
@@ -270,7 +274,7 @@ public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable Clas
270
274
}
271
275
272
276
/**
273
- * Return a {@link SpringFactoriesLoader} instance that will load and
277
+ * Create a {@link SpringFactoriesLoader} instance that will load and
274
278
* instantiate the factory implementations from
275
279
* {@value #FACTORIES_RESOURCE_LOCATION}, using the default class loader.
276
280
* @return a {@link SpringFactoriesLoader} instance
@@ -282,7 +286,7 @@ public static SpringFactoriesLoader forDefaultResourceLocation() {
282
286
}
283
287
284
288
/**
285
- * Return a {@link SpringFactoriesLoader} instance that will load and
289
+ * Create a {@link SpringFactoriesLoader} instance that will load and
286
290
* instantiate the factory implementations from
287
291
* {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader.
288
292
* @param classLoader the ClassLoader to use for loading resources; can be
@@ -296,9 +300,10 @@ public static SpringFactoriesLoader forDefaultResourceLocation(@Nullable ClassLo
296
300
}
297
301
298
302
/**
299
- * Return a {@link SpringFactoriesLoader} instance that will load and
303
+ * Create a {@link SpringFactoriesLoader} instance that will load and
300
304
* instantiate the factory implementations from the given location, using
301
305
* the default class loader.
306
+ * @param resourceLocation the resource location to look for factories
302
307
* @return a {@link SpringFactoriesLoader} instance
303
308
* @since 6.0
304
309
* @see #forResourceLocation(ClassLoader, String)
@@ -308,11 +313,12 @@ public static SpringFactoriesLoader forResourceLocation(String resourceLocation)
308
313
}
309
314
310
315
/**
311
- * Return a {@link SpringFactoriesLoader} instance that will load and
316
+ * Create a {@link SpringFactoriesLoader} instance that will load and
312
317
* instantiate the factory implementations from the given location, using
313
318
* the given class loader.
314
319
* @param classLoader the ClassLoader to use for loading resources; can be
315
320
* {@code null} to use the default
321
+ * @param resourceLocation the resource location to look for factories
316
322
* @return a {@link SpringFactoriesLoader} instance
317
323
* @since 6.0
318
324
* @see #forResourceLocation(String)
@@ -355,6 +361,7 @@ private static List<String> toDistinctUnmodifiableList(String factoryType, List<
355
361
356
362
/**
357
363
* Internal instantiator used to create the factory instance.
364
+ * @since 6.0
358
365
* @param <T> the instance implementation type
359
366
*/
360
367
static final class FactoryInstantiator <T > {
@@ -434,6 +441,7 @@ private static Constructor<?> findDeclaredConstructor(Class<?> factoryImplementa
434
441
435
442
/**
436
443
* Nested class to avoid a hard dependency on Kotlin at runtime.
444
+ * @since 6.0
437
445
*/
438
446
private static class KotlinDelegate {
439
447
@@ -625,48 +633,50 @@ public interface FailureHandler {
625
633
*/
626
634
void handleFailure (Class <?> factoryType , String factoryImplementationName , Throwable failure );
627
635
636
+
628
637
/**
629
- * Return a new {@link FailureHandler} that handles
630
- * errors by throwing an {@link IllegalArgumentException}.
638
+ * Create a new {@link FailureHandler} that handles errors by throwing an
639
+ * {@link IllegalArgumentException}.
631
640
* @return a new {@link FailureHandler} instance
641
+ * @see #throwing(BiFunction)
632
642
*/
633
643
static FailureHandler throwing () {
634
644
return throwing (IllegalArgumentException ::new );
635
645
}
636
646
637
647
/**
638
- * Return a new {@link FailureHandler} that handles errors by throwing an
648
+ * Create a new {@link FailureHandler} that handles errors by throwing an
639
649
* exception.
640
650
* @param exceptionFactory factory used to create the exception
641
651
* @return a new {@link FailureHandler} instance
642
652
*/
643
653
static FailureHandler throwing (BiFunction <String , Throwable , ? extends RuntimeException > exceptionFactory ) {
644
- return handleMessage ((message , failure ) -> {
645
- throw exceptionFactory .apply (message .get (), failure );
654
+ return handleMessage ((messageSupplier , failure ) -> {
655
+ throw exceptionFactory .apply (messageSupplier .get (), failure );
646
656
});
647
657
}
648
658
649
659
/**
650
- * Return a new {@link FailureHandler} that handles errors by logging trace
660
+ * Create a new {@link FailureHandler} that handles errors by logging trace
651
661
* messages.
652
- * @param logger the logger used to log message
662
+ * @param logger the logger used to log messages
653
663
* @return a new {@link FailureHandler} instance
654
664
*/
655
665
static FailureHandler logging (Log logger ) {
656
- return handleMessage ((message , failure ) -> logger .trace (LogMessage .of (message ), failure ));
666
+ return handleMessage ((messageSupplier , failure ) -> logger .trace (LogMessage .of (messageSupplier ), failure ));
657
667
}
658
668
659
669
/**
660
- * Return a new {@link FailureHandler} that handles errors using a standard
670
+ * Create a new {@link FailureHandler} that handles errors using a standard
661
671
* formatted message.
662
672
* @param messageHandler the message handler used to handle the problem
663
673
* @return a new {@link FailureHandler} instance
664
674
*/
665
675
static FailureHandler handleMessage (BiConsumer <Supplier <String >, Throwable > messageHandler ) {
666
676
return (factoryType , factoryImplementationName , failure ) -> {
667
- Supplier <String > message = () -> "Unable to instantiate factory class [%s] for factory type [%s]"
677
+ Supplier <String > messageSupplier = () -> "Unable to instantiate factory class [%s] for factory type [%s]"
668
678
.formatted (factoryImplementationName , factoryType .getName ());
669
- messageHandler .accept (message , failure );
679
+ messageHandler .accept (messageSupplier , failure );
670
680
};
671
681
}
672
682
0 commit comments