亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean

 更新時(shí)間:2022年04月28日 16:40:41   作者:CoderJ  
這篇文章主要介紹了SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下

1. 前言

SpringBoot在包掃描時(shí),并不會(huì)掃描子模塊下的內(nèi)容,這樣就使得我們的子模塊中的Bean無(wú)法注入到Spring容器中。SpringBoot就為我們提供了spring.factories這個(gè)文件,讓我們可以輕松的將子模塊的Bean注入到我們的Spring容器中,本篇文章我們就一起探究一下spring.factories 跨模塊實(shí)例化Bean的原理。

我們?cè)?a href="http://chabaoo.cn/article/246336.htm" rel="nofollow" target="_blank">SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?文章中也講到構(gòu)建自己構(gòu)建starter,其中spring.factories就起到重要的作用,我們是通過(guò)spring.factories讓starer項(xiàng)目中的Bean注入到Web模塊的Spring容器中。本篇文章就來(lái)探究一下spring.factories文件,更深層次的東西,以及我們是如何借助該文件實(shí)例化Bean的。

2. 配置

spring.factories文件一般都是配置在src/main/resources/META-INF/ 目錄下。

也就是說(shuō)我們?cè)贗DEA新建的SpringBoot項(xiàng)目或者M(jìn)aven項(xiàng)目的資源文件resources目錄下新建一個(gè)META-INF文件夾,再建一個(gè)spring.factories文件即可,新建的文件沒(méi)有問(wèn)題的化,一般IDEA都能自動(dòng)識(shí)別,如下圖所示。

spring.factories 的文件內(nèi)容就是接口對(duì)應(yīng)其實(shí)現(xiàn)類(lèi),實(shí)現(xiàn)類(lèi)可以有多個(gè)

文件內(nèi)容必須是kv形式,即properties類(lèi)型

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhj.config.AutoConfiguration

如其一個(gè)接口有多個(gè)實(shí)現(xiàn),如下配置:

org.springframework.boot.logging.LoggingSystemFactory=\
org.springframework.boot.logging.logback.LogbackLoggingSystem.Factory,\
org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.Factory,\
org.springframework.boot.logging.java.JavaLoggingSystem.Factory

3. 原理

spring -core 中定義了SpringFactoriesLoader 類(lèi),這個(gè)類(lèi)就是讓spring.factories文件發(fā)揮作用的類(lèi)。SpringFactoriesLoader類(lèi)的作用就是檢索META-INF/spring.factories文件,并獲取指定接口將其實(shí)現(xiàn)實(shí)例化。 在這個(gè)類(lèi)中定義了兩個(gè)對(duì)外的方法:

  • loadFactories 根據(jù)給定的接口類(lèi)獲取其實(shí)現(xiàn)類(lèi)的實(shí)例,這個(gè)方法返回的是對(duì)象列表
  • loadFactoryNames 根據(jù)給定的類(lèi)型加載類(lèi)路徑的全限定類(lèi)名,這個(gè)方法返回的是全限定類(lèi)名的列表。

源碼如下:

public final class SpringFactoriesLoader {
    
 ? ?// 文件位置
    public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
    
 ? ?// 緩存
    private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>();
?
?
    private SpringFactoriesLoader() {
    }
?
?
    /**
     * 根據(jù)給定的類(lèi)型加載并實(shí)例化工廠的實(shí)現(xiàn)類(lèi)
     */
    public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader) {
        Assert.notNull(factoryType, "'factoryType' must not be null");
        // 獲取類(lèi)加載器
        ClassLoader classLoaderToUse = classLoader;
        if (classLoaderToUse == null) {
            classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
        }
        // 加載類(lèi)的全限定名
        List<String> factoryImplementationNames = loadFactoryNames(factoryType, classLoaderToUse);
        if (logger.isTraceEnabled()) {
            logger.trace("Loaded [" + factoryType.getName() + "] names: " + factoryImplementationNames);
        }
        List<T> result = new ArrayList<>(factoryImplementationNames.size());
        for (String factoryImplementationName : factoryImplementationNames) {
            // 實(shí)例化Bean,并將Bean放入到List集合中
            result.add(instantiateFactory(factoryImplementationName, factoryType, classLoaderToUse));
        }
        AnnotationAwareOrderComparator.sort(result);
        return result;
    }
    
    /**
     * 根據(jù)給定的類(lèi)型加載類(lèi)路徑的全限定類(lèi)名
     */
    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
        
 ? ? ? ?// 獲取工廠類(lèi)型名稱(chēng)
        String factoryTypeName = factoryType.getName();
        // 加載所有META-INF/spring.factories中的value
        return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
    }
    
    private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
        // 根據(jù)類(lèi)加載器從緩存中獲取,如果緩存中存在,就直接返回,如果不存在就去加載
        MultiValueMap<String, String> result = cache.get(classLoader);
        if (result != null) {
            return result;
        }
    
        try {
            // 獲取所有jar中classpath路徑下的META-INF/spring.factories
            Enumeration<URL> urls = (classLoader != null ?
                    classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
                    ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
            result = new LinkedMultiValueMap<>();
            // 遍歷所有的META-INF/spring.factories
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                UrlResource resource = new UrlResource(url);
                // 將META-INF/spring.factories中的key value加載為Prpperties對(duì)象
                Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                for (Map.Entry<?, ?> entry : properties.entrySet()) {
                    // key就是接口的類(lèi)名稱(chēng)
                    String factoryTypeName = ((String) entry.getKey()).trim();
                    for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
                        // 以factoryTypeName為key,實(shí)現(xiàn)類(lèi)為value放入map集合中
                        result.add(factoryTypeName, factoryImplementationName.trim());
                    }
                }
            }
            // 加入緩存
            cache.put(classLoader, result);
            return result;
        }
        catch (IOException ex) {
            throw new IllegalArgumentException("Unable to load factories from location [" +
                    FACTORIES_RESOURCE_LOCATION + "]", ex);
        }
    }
    
    // 通過(guò)反射實(shí)例化Bean對(duì)象
    @SuppressWarnings("unchecked")
    private static <T> T instantiateFactory(String factoryImplementationName, Class<T> factoryType, ClassLoader classLoader) {
        try {
            Class<?> factoryImplementationClass = ClassUtils.forName(factoryImplementationName, classLoader);
            if (!factoryType.isAssignableFrom(factoryImplementationClass)) {
                throw new IllegalArgumentException(
                        "Class [" + factoryImplementationName + "] is not assignable to factory type [" + factoryType.getName() + "]");
            }
            return (T) ReflectionUtils.accessibleConstructor(factoryImplementationClass).newInstance();
        }
        catch (Throwable ex) {
            throw new IllegalArgumentException(
                "Unable to instantiate factory class [" + factoryImplementationName + "] for factory type [" + factoryType.getName() + "]",
                ex);
        }
    }
?
}

4. 總結(jié)

Spring通過(guò)SpringFactoriesLoader實(shí)例化Bean的過(guò)程

  • 獲取SpringFactoriesLoader對(duì)應(yīng)的類(lèi)加載器
  • 查找緩存,查看緩存中是否已經(jīng)讀取到所有jar中classpath路徑下的META-INF/spring.factories的內(nèi)容
  • 如果緩存已經(jīng)存在,根據(jù)/spring.factories文件中配置的全限定類(lèi)名通過(guò)反射實(shí)例化Bean
  • 如果緩存中沒(méi)有值,則掃描所有jar中的這個(gè)META-INF/spring.factories文件,并將其以讀取到緩存中,并返回這個(gè)配置列表
  • 然后根據(jù)這個(gè)全限定類(lèi)名的列表再通過(guò)反射實(shí)例化Bean

到此這篇關(guān)于SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean的文章就介紹到這了,更多相關(guān)SpringBoot實(shí)例化Bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論