|
| 1 | + ## 工厂加载机制解析 |
| 2 | + |
| 3 | +### SpringFactoriesLoader 介绍 |
| 4 | + |
| 5 | +> org.springframework.core.io.support.SpringFactoriesLoader |
| 6 | + |
| 7 | +```java |
| 8 | +/** |
| 9 | + * General purpose factory loading mechanism for internal use within the framework. |
| 10 | + * |
| 11 | + * <p>{@code SpringFactoriesLoader} {@linkplain #loadFactories loads} and instantiates |
| 12 | + * factories of a given type from {@value #FACTORIES_RESOURCE_LOCATION} files which |
| 13 | + * may be present in multiple JAR files in the classpath. The {@code spring.factories} |
| 14 | + * file must be in {@link Properties} format, where the key is the fully qualified |
| 15 | + * name of the interface or abstract class, and the value is a comma-separated list of |
| 16 | + * implementation class names. For example: |
| 17 | + * |
| 18 | + * <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre> |
| 19 | + * |
| 20 | + * where {@code example.MyService} is the name of the interface, and {@code MyServiceImpl1} |
| 21 | + * and {@code MyServiceImpl2} are two implementations. |
| 22 | + * |
| 23 | + * @author Arjen Poutsma |
| 24 | + * @author Juergen Hoeller |
| 25 | + * @author Sam Brannen |
| 26 | + * @since 3.2 |
| 27 | + */ |
| 28 | +public final class SpringFactoriesLoader { |
| 29 | +``` |
| 30 | + |
| 31 | +- 框架内部使用的通用工厂加载机制 |
| 32 | +- 从classpath 下多个jar包特定的位置读取文件并初始化类 |
| 33 | +- 文件内容必须是kv形式,即properties类型 |
| 34 | +- key是全限定名(抽象类|接口)、value是实现,多个实现用逗号分隔 |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
0 commit comments