Skip to content

Commit b611157

Browse files
committed
Polish SpringFactoriesLoader
1 parent 1bbc564 commit b611157

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

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

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public class SpringFactoriesLoader {
118118
* Create a new {@link SpringFactoriesLoader} instance.
119119
* @param classLoader the classloader used to instantiate the factories
120120
* @param factories a map of factory class name to implementation class names
121+
* @since 6.0
121122
*/
122123
protected SpringFactoriesLoader(@Nullable ClassLoader classLoader, Map<String, List<String>> factories) {
123124
this.classLoader = classLoader;
@@ -127,10 +128,10 @@ protected SpringFactoriesLoader(@Nullable ClassLoader classLoader, Map<String, L
127128

128129
/**
129130
* 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.
132133
* <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(...)}
134135
* with a custom {@link ArgumentResolver ArgumentResolver} and/or
135136
* {@link FailureHandler FailureHandler}.
136137
* <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
139140
* @param factoryType the interface or abstract class representing the factory
140141
* @throws IllegalArgumentException if any factory implementation class cannot
141142
* be loaded or if an error occurs while instantiating any factory
143+
* @since 6.0
142144
*/
143145
public <T> List<T> load(Class<T> factoryType) {
144146
return load(factoryType, NO_ARGUMENT_RESOLVER, NO_FAILURE_HANDLER);
145147
}
146148

147149
/**
148150
* 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.
151153
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
152154
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
153155
* 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
164166

165167
/**
166168
* 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.
169171
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
170172
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
171173
* discovered for a given factory type, only one instance of the duplicated
172174
* 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.
175177
* @param factoryType the interface or abstract class representing the factory
176178
* @param failureHandler strategy used to handle factory instantiation failures
177179
* @since 6.0
@@ -182,15 +184,15 @@ public <T> List<T> load(Class<T> factoryType, @Nullable FailureHandler failureHa
182184

183185
/**
184186
* 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
187189
* failure handler.
188190
* <p>The returned factories are sorted through {@link AnnotationAwareOrderComparator}.
189191
* <p>As of Spring Framework 5.3, if duplicate implementation class names are
190192
* discovered for a given factory type, only one instance of the duplicated
191193
* 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.
194196
* @param factoryType the interface or abstract class representing the factory
195197
* @param argumentResolver strategy used to resolve constructor arguments by their type
196198
* @param failureHandler strategy used to handle factory instantiation failures
@@ -233,6 +235,7 @@ protected <T> T instantiateFactory(String implementationName, Class<T> type,
233235
}
234236
}
235237

238+
236239
/**
237240
* Load and instantiate the factory implementations of the given type from
238241
* {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader.
@@ -244,7 +247,8 @@ protected <T> T instantiateFactory(String implementationName, Class<T> type,
244247
* {@link FailureHandler} support use {@link #forDefaultResourceLocation(ClassLoader)}
245248
* to obtain a {@link SpringFactoriesLoader} instance.
246249
* @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)
248252
* @throws IllegalArgumentException if any factory implementation class cannot
249253
* be loaded or if an error occurs while instantiating any factory
250254
*/
@@ -270,7 +274,7 @@ public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable Clas
270274
}
271275

272276
/**
273-
* Return a {@link SpringFactoriesLoader} instance that will load and
277+
* Create a {@link SpringFactoriesLoader} instance that will load and
274278
* instantiate the factory implementations from
275279
* {@value #FACTORIES_RESOURCE_LOCATION}, using the default class loader.
276280
* @return a {@link SpringFactoriesLoader} instance
@@ -282,7 +286,7 @@ public static SpringFactoriesLoader forDefaultResourceLocation() {
282286
}
283287

284288
/**
285-
* Return a {@link SpringFactoriesLoader} instance that will load and
289+
* Create a {@link SpringFactoriesLoader} instance that will load and
286290
* instantiate the factory implementations from
287291
* {@value #FACTORIES_RESOURCE_LOCATION}, using the given class loader.
288292
* @param classLoader the ClassLoader to use for loading resources; can be
@@ -296,9 +300,10 @@ public static SpringFactoriesLoader forDefaultResourceLocation(@Nullable ClassLo
296300
}
297301

298302
/**
299-
* Return a {@link SpringFactoriesLoader} instance that will load and
303+
* Create a {@link SpringFactoriesLoader} instance that will load and
300304
* instantiate the factory implementations from the given location, using
301305
* the default class loader.
306+
* @param resourceLocation the resource location to look for factories
302307
* @return a {@link SpringFactoriesLoader} instance
303308
* @since 6.0
304309
* @see #forResourceLocation(ClassLoader, String)
@@ -308,11 +313,12 @@ public static SpringFactoriesLoader forResourceLocation(String resourceLocation)
308313
}
309314

310315
/**
311-
* Return a {@link SpringFactoriesLoader} instance that will load and
316+
* Create a {@link SpringFactoriesLoader} instance that will load and
312317
* instantiate the factory implementations from the given location, using
313318
* the given class loader.
314319
* @param classLoader the ClassLoader to use for loading resources; can be
315320
* {@code null} to use the default
321+
* @param resourceLocation the resource location to look for factories
316322
* @return a {@link SpringFactoriesLoader} instance
317323
* @since 6.0
318324
* @see #forResourceLocation(String)
@@ -355,6 +361,7 @@ private static List<String> toDistinctUnmodifiableList(String factoryType, List<
355361

356362
/**
357363
* Internal instantiator used to create the factory instance.
364+
* @since 6.0
358365
* @param <T> the instance implementation type
359366
*/
360367
static final class FactoryInstantiator<T> {
@@ -434,6 +441,7 @@ private static Constructor<?> findDeclaredConstructor(Class<?> factoryImplementa
434441

435442
/**
436443
* Nested class to avoid a hard dependency on Kotlin at runtime.
444+
* @since 6.0
437445
*/
438446
private static class KotlinDelegate {
439447

@@ -625,48 +633,50 @@ public interface FailureHandler {
625633
*/
626634
void handleFailure(Class<?> factoryType, String factoryImplementationName, Throwable failure);
627635

636+
628637
/**
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}.
631640
* @return a new {@link FailureHandler} instance
641+
* @see #throwing(BiFunction)
632642
*/
633643
static FailureHandler throwing() {
634644
return throwing(IllegalArgumentException::new);
635645
}
636646

637647
/**
638-
* Return a new {@link FailureHandler} that handles errors by throwing an
648+
* Create a new {@link FailureHandler} that handles errors by throwing an
639649
* exception.
640650
* @param exceptionFactory factory used to create the exception
641651
* @return a new {@link FailureHandler} instance
642652
*/
643653
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);
646656
});
647657
}
648658

649659
/**
650-
* Return a new {@link FailureHandler} that handles errors by logging trace
660+
* Create a new {@link FailureHandler} that handles errors by logging trace
651661
* messages.
652-
* @param logger the logger used to log message
662+
* @param logger the logger used to log messages
653663
* @return a new {@link FailureHandler} instance
654664
*/
655665
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));
657667
}
658668

659669
/**
660-
* Return a new {@link FailureHandler} that handles errors using a standard
670+
* Create a new {@link FailureHandler} that handles errors using a standard
661671
* formatted message.
662672
* @param messageHandler the message handler used to handle the problem
663673
* @return a new {@link FailureHandler} instance
664674
*/
665675
static FailureHandler handleMessage(BiConsumer<Supplier<String>, Throwable> messageHandler) {
666676
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]"
668678
.formatted(factoryImplementationName, factoryType.getName());
669-
messageHandler.accept(message, failure);
679+
messageHandler.accept(messageSupplier, failure);
670680
};
671681
}
672682

0 commit comments

Comments
 (0)