SPRINGBOOT启动原理(基于2.x版本)(二)-SpringFactoriesLoader

版本

版本:3.0.1
上一篇:SPRINGBOOT启动原理(基于3.x版本)(一)

SpringFactoriesLoader

引入

从上一篇可以看到,代码中频繁地使用到了SpringFactoriesLoader这个类,大致能看出来是为了拿类的,今天我们详细的研究一下。

介绍

看一下官网的介绍:

General purpose factory loading mechanism for internal use within the framework.
SpringFactoriesLoader loads and instantiates factories of a given type from “META-INF/spring.factories” files which may be present in multiple JAR files in the classpath. The spring.factories file must be in Properties format, where the key is the fully qualified name of the interface or abstract class, and the value is a comma-separated list of implementation class names. For example:

example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
where example.MyService is the name of the interface, and MyServiceImpl1 and MyServiceImpl2 are two implementations.

翻译一下:
用于框架内部使用的通用工厂加载机制。

SpringFactoriesLoader从“META-INF/spring.factories”文件加载和实例化给定类型的工厂,这些文件可能存在于类路径中的多个 JAR 文件中。该spring.factories 文件必须采用Properties格式,其中key是接口或抽象类的完全限定名称,value为相应的实现类,当存在多个实现类时,用“,”进行分割。
然后举了个例子

代码

最常用到的方法就是

SpringFactoriesLoader.loadFactories

我们自己写一个来试一试,先看下目录结构:
在这里插入图片描述
具体代码:

public interface Fruit {
    void Color();
}

public class Banana implements Fruit{
    public Banana(){
        System.out.println("A Banana");
    }

    @Override
    public void Color() {
        System.out.println("yello");
    }
}

public class Orange implements Fruit{

    public Orange(){
        System.out.println("An Orange");
    }

    @Override
    public void Color() {
        System.out.println("orange");
    }
}

spring.factories

com.example.mavendemo.loadertest.Fruit=com.example.mavendemo.loadertest.Banana,com.example.mavendemo.loadertest.Orange

测试方法:

@SpringBootTest
class FruitTest {

    @Test
    public void testLoadFactoryNames() {
        //获取所有META-INF/spring.factories中的value值
        List<String> applicationContextInitializers = SpringFactoriesLoader.loadFactoryNames(Fruit.class, ClassUtils.getDefaultClassLoader());
        for (String applicationContextInitializer : applicationContextInitializers) {
            System.out.println(applicationContextInitializer);
        }
    }

    @Test
    public void testLoadFactories() {
        //实例化所有在META-INF/spring.factories配置的且实现BeanInfoFactory接口的类
        List<Fruit> fruitFactories = SpringFactoriesLoader.loadFactories(Fruit.class, ClassUtils.getDefaultClassLoader());

        for (Fruit fruit : fruitFactories) {
            System.out.println(fruitFactories);
        }
    }
}

参考

Spring SpringFactoriesLoader

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盖丽男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值