最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中的DeferredImportSelector實現(xiàn)詳解

 更新時間:2024年01月03日 09:15:46   作者:it_lihongmin  
這篇文章主要介紹了Spring中的DeferredImportSelector實現(xiàn)詳解,兩個官方的實現(xiàn)類AutoConfigurationImportSelector和ImportAutoConfigurationImportSelector都是Spring Boot后新增的實現(xiàn),需要的朋友可以參考下

前言

在分析DeferredImportSelector之前,根據(jù)名字Deferred(延遲的)ImportSelector。

ImportSelector則是將selectImports返回的字符串數(shù)組,注冊成為Bean

那么有幾個問題:

1)、怎么延遲的;

2)、執(zhí)行時機,或者在什莫時候被調(diào)用的;

3)、返回后的字符串數(shù)組,怎么注冊成Bean(或者Beandefinition)。

一、DeferredImportSelector類結(jié)構(gòu)和接口調(diào)用時機

之前在分析Spring源碼時,并沒有分析DeferredImportSelector類型,其兩個官方的實現(xiàn)類AutoConfigurationImportSelectorImportAutoConfigurationImportSelector都是Spring Boot后新增的實現(xiàn)。

public interface DeferredImportSelector extends ImportSelector {
    default Class<? extends Group> getImportGroup() {
        return null;
    }
    interface Group {
        void process(AnnotationMetadata metadata, DeferredImportSelector selector);
        Iterable<Entry> selectImports();
        class Entry {
            private final AnnotationMetadata metadata;
            private final String importClassName;
            public Entry(AnnotationMetadata metadata, String importClassName) {
                this.metadata = metadata;
                this.importClassName = importClassName;
            }
        }
    }
}

DeferredImportSelector的回調(diào)時機發(fā)生在ConfigurationClassPostProcessor處理階段,但是在處理DeferredImportSelector類型時并沒有回調(diào)從頂層接口ImportSelector的selectImports(AnnotationMetadata importingClassMetadata)方法,則按道理AutoConfigurationImportSelector(自動裝配)的該接口只需要寫一個空方法即可。

執(zhí)行該回調(diào)方法的先后順序為 Group#process 和 Group#selectImports,回調(diào)時機為:

AbstractApplicationContext #refresh

AbstractApplicationContext #invokeBeanFactoryPostProcessors

PostProcessorRegistrationDelegate #invokeBeanFactoryPostProcessors

PostProcessorRegistrationDelegate #invokeBeanDefinitionRegistryPostProcessors

ConfigurationClassPostProcessor #postProcessBeanDefinitionRegistry

ConfigurationClassPostProcessor #processConfigBeanDefinitions

ConfigurationClassParser #parse

ConfigurationClassParser$DeferredImportSelectorHandler #process (下面都為代理對象處理)

ConfigurationClassParser$DeferredImportSelectorGroupingHandler #processGroupImports

ConfigurationClassParser$DeferredImportSelectorGrouping #getImports

之前在分析ConfigurationClassPostProcessor的處理過程的時候并沒有注意到DeferredImportSelector類型的處理過程

按照上面的調(diào)用trace繼續(xù)分析,ConfigurationClassParser #parse方法,如下:

public void parse(Set<BeanDefinitionHolder> configCandidates) {
    for (BeanDefinitionHolder holder : configCandidates) {
        BeanDefinition bd = holder.getBeanDefinition();
        try {
            if (bd instanceof AnnotatedBeanDefinition) {
                parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
            }
            else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
                parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
            }
            else {
                parse(bd.getBeanClassName(), holder.getBeanName());
            }
        }
        catch (BeanDefinitionStoreException ex) {
            throw ex;
        }
        catch (Throwable ex) {
            throw new BeanDefinitionStoreException(
                    "Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
        }
    }
 
    this.deferredImportSelectorHandler.process();
}

其中for循環(huán)之前分析過了,會將可能存在的Bean進行注入,并且繼續(xù)進行解析肯能的注解情況,比如:有一個@Component標注的類,上面還有@Import等情況。而deferredImportSelectorHandler.process()則是對DeferredImportSelector類型的處理。

看一下DeferredImportSelectorHandler deferredImportSelectorHandler的結(jié)構(gòu):

private List<DeferredImportSelectorHolder> deferredImportSelectors = new ArrayList<>();

唯一的屬性就是存放DeferredImportSelector類型的List,還有handle和process兩個方法。而DeferredImportSelectorHolder的結(jié)構(gòu)為:

private static class DeferredImportSelectorHolder {
    // 注解所在類
    private final ConfigurationClass configurationClass;
    // DeferredImportSelector類型,比如:AutoConfigurationImportSelector
    private final DeferredImportSelector importSelector;
}

DeferredImportSelector的兩個官方實現(xiàn)都是Spring Boot的,看到上面的結(jié)構(gòu)理解起來比較抽象。在啟動Spring Boot項目時,默認只會注入一對值:

configurationClass為被@SpringBootApplication標注的啟動類;

DeferredImportSelector為AutoConfigurationImportSelector類型。

二、DeferredImportSelectorHandler的process方法

繼續(xù)deferredImportSelectorHandler.process()分析:

public void process() {    List<DeferredImportSelectorHolder> deferredImports = this.deferredImportSelectors;    this.deferredImportSelectors = null;    try {        if (deferredImports != null) {            DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();            deferredImports.sort(DEFERRED_IMPORT_COMPARATOR);            deferredImports.forEach(handler::register);            handler.processGroupImports();        }    } finally {        this.deferredImportSelectors = new ArrayList<>();    }}

1)、創(chuàng)建DeferredImportSelectorGroupingHandler對象,對DeferredImportSelectorHolder類型List進行排序,然后遍歷調(diào)用DeferredImportSelectorGroupingHandler的register方法,將DeferredImportSelectorHolder數(shù)據(jù)set到DeferredImportSelectorGroupingHandler內(nèi)。

2)、調(diào)用hander的processGroupImports方法進行解析注冊。

3)、finally,將容器置為空。

1、DeferredImportSelectorGroupingHandler結(jié)構(gòu)

首先所有的解析過程交給DeferredImportSelectorGroupingHandler進行處理,先看看其內(nèi)部結(jié)構(gòu)。

private class DeferredImportSelectorGroupingHandler {
 
    private final Map<Object, DeferredImportSelectorGrouping> groupings = new LinkedHashMap<>();
 
    private final Map<AnnotationMetadata, ConfigurationClass> configurationClasses = new HashMap<>();
}

有兩個字段,通過register方法挨個進行register(相當于set方法)到上兩個字段中, 最后統(tǒng)一調(diào)用processGroupImports方法進行處理。

1)、configurationClasses比較清楚,存放的是注解信息和標記注解的類(比如:當前為@SpringBootApplication鎖在的類的注解信息被封裝為StrandardAnnotationMetadata)。

2)、group中存儲的key為DeferredImportSelector.Group或者DeferredImportSelectorHolder對象(因為是調(diào)用DeferredImportSelector的getImportGroup方法獲取的,可能為null,所以key為Object類型)。value為DeferredImportSelectorGrouping類型,再看看其結(jié)構(gòu)(還是比較清晰的,只是需要理解幾層的關(guān)系):

private static class DeferredImportSelectorGrouping {
 
    private final DeferredImportSelector.Group group;
 
    private final List<DeferredImportSelectorHolder> deferredImports = new ArrayList<>();
}

2、DeferredImportSelectorGroupingHandler的register方法

public void register(DeferredImportSelectorHolder deferredImport) {
    Class<? extends Group> group = deferredImport.getImportSelector()
            .getImportGroup();
    DeferredImportSelectorGrouping grouping = this.groupings.computeIfAbsent(
            (group != null ? group : deferredImport),
            key -> new DeferredImportSelectorGrouping(createGroup(group)));
    grouping.add(deferredImport);
    this.configurationClasses.put(deferredImport.getConfigurationClass().getMetadata(),
            deferredImport.getConfigurationClass());
}

1)、調(diào)用DeferredImportSelector.Group的getImportGroup方法,獲取返回的DeferredImportSelector.Group。

2)、grouping的put方法,Group為null則設(shè)置傳入的DeferredImportSelectorHolder 本身。value為new的DeferredImportSelectorHolder 類型。

調(diào)用了createGroup方法,通過Class和反射創(chuàng)建對象;并且傳入ConfigurationClassParser的environment、resourceLoader、registry對象,在反射創(chuàng)建完對象后對部分Aware方法進行了回調(diào)(BeanClassLoaderAware、BeanFactoryAware、EnvironmentAware、ResourceLoaderAware)。

沒搞懂為什么要回調(diào),因為Bean的生命周期本身就會進行回調(diào)。后面debugger發(fā)現(xiàn),AutoConfigurationImportSelector本身實現(xiàn)了那幾個接口,AutoConfigurationImportSelector.AutoConfigurationGroup也實現(xiàn)了那幾個接口。知道當前的調(diào)用時機還沒有進行g(shù)etBean操作,不能執(zhí)行生命周期方法(回調(diào),設(shè)置BeanFactory等),但是執(zhí)行DeferredImportSelector的process和selectImports方法時可能需要使用到上面四個AbstractApplicationContext級別的內(nèi)置對象,則在此處回調(diào)賦值。

3)、上面先進行了put方法,返回了DeferredImportSelectorGrouping對象。再將DeferredImportSelectorHolder添加進去。

4)、為List<DeferredImportSelectorHolder> deferredImports 添加值。

3、DeferredImportSelectorGroupingHandler的processGroupImports方法

public void processGroupImports() {
    for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
        grouping.getImports().forEach(entry -> {
            ConfigurationClass configurationClass = this.configurationClasses.get(
                    entry.getMetadata());
            try {
                processImports(configurationClass, asSourceClass(configurationClass),
                        asSourceClasses(entry.getImportClassName()), false);
            } catch (BeanDefinitionStoreException ex) {
                // 省略異常代碼
            }
        });
    }
}

主要有兩步

1)、調(diào)用getImports方法獲取Iterable<Group.Entry>

public Iterable<Group.Entry> getImports() {
    for (DeferredImportSelectorHolder deferredImport : this.deferredImports) {
        this.group.process(deferredImport.getConfigurationClass().getMetadata(),
                deferredImport.getImportSelector());
    }
    return this.group.selectImports();
}

之前做了很多準備工作,當前需要通過調(diào)用DeferredImportSelector.Group的process和selectImports方法,返回Iterable<DeferredImportSelector.Group.Entry> 類型對象,后續(xù)調(diào)用統(tǒng)一的processImports方法進行處理。

2)、遍歷調(diào)用ConfigurationClassParser的processImports方法,將返回的Class類型進行解析并注冊成Bean(如果該Class上有其他注解,也會遞歸進行解析注冊完成,比如該類上還有@ComponentScan注解)。

到此這篇關(guān)于Spring中的DeferredImportSelector實現(xiàn)詳解的文章就介紹到這了,更多相關(guān)DeferredImportSelector實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot詳細講解多個配置文件的配置流程

    SpringBoot詳細講解多個配置文件的配置流程

    SpringBoot項目是一個標準的Maven項目,它的配置文件需要放在src/main/resources/下,其文件名必須為application,其存在兩種文件形式,分別是properties和yaml(或者yml)文件
    2022-06-06
  • Java枚舉類型enum的詳解及使用

    Java枚舉類型enum的詳解及使用

    這篇文章主要介紹了Java枚舉類型enum的詳解及使用的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 關(guān)于CommandLineRunner的使用詳解

    關(guān)于CommandLineRunner的使用詳解

    本文介紹了如何在SpringBoot項目啟動時使用CommandLineRunner和ApplicationRunner接口進行數(shù)據(jù)預(yù)加載或操作,通過實現(xiàn)這兩個接口,可以在項目啟動時執(zhí)行特定的任務(wù),同時,還展示了如何使用@Order注解來控制多個實現(xiàn)類的加載順序
    2024-12-12
  • Java try()語句實現(xiàn)try-with-resources異常管理機制操作

    Java try()語句實現(xiàn)try-with-resources異常管理機制操作

    這篇文章主要介紹了Java try()語句實現(xiàn)try-with-resources異常管理機制操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java 多線程實現(xiàn)在線咨詢(udp)

    java 多線程實現(xiàn)在線咨詢(udp)

    這篇文章主要介紹了java 多線程實現(xiàn)在線咨詢(udp)的示例,幫助大家更好的理解和學(xué)習(xí)Java 網(wǎng)絡(luò)編程的相關(guān)內(nèi)容,感興趣的朋友可以了解下
    2020-11-11
  • spring boot結(jié)合Redis實現(xiàn)工具類的方法示例

    spring boot結(jié)合Redis實現(xiàn)工具類的方法示例

    這篇文章主要介紹了spring boot結(jié)合Redis實現(xiàn)工具類的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 實例詳解SpringBoot默認的JSON解析方案

    實例詳解SpringBoot默認的JSON解析方案

    JSON數(shù)據(jù)現(xiàn)在是我們開發(fā)中用的最多的,百分之九十的數(shù)據(jù)都是通過JSON方式進行傳輸,下面這篇文章主要給大家介紹了關(guān)于SpringBoot默認的JSON解析方案的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Mybatis實現(xiàn)SQL映射的兩種方法(xml文件形式和注解形式)

    Mybatis實現(xiàn)SQL映射的兩種方法(xml文件形式和注解形式)

    這篇文章主要介紹了Mybatis實現(xiàn)SQL映射的兩種方法(xml文件形式和注解形式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法

    java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法

    這篇文章主要介紹了java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法,涉及java字符解析操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Spring注入bean的四種方式總結(jié)

    Spring注入bean的四種方式總結(jié)

    我們談到Spring的時候一定會提到IOC容器、DI依賴注入,Spring通過將一個個類標注為Bean的方法注入到IOC容器中,達到了控制反轉(zhuǎn)的效果,Spring幫我們實現(xiàn)了一種通過注解來實現(xiàn)注入的方法,所以本文為大家介紹了Spring注入bean的四種方式,需要的朋友可以參考下
    2025-11-11

最新評論

阳原县| 建湖县| 沂南县| 望谟县| 桐梓县| 福州市| 张家口市| 双柏县| 田东县| 常州市| 阿图什市| 玛多县| 盐边县| 锡林浩特市| 乌审旗| 曲阜市| 安多县| 沾化县| 营山县| 嘉禾县| 抚州市| 色达县| 沧源| 洪泽县| 固阳县| 太白县| 崇左市| 西峡县| 磐石市| 眉山市| 丹巴县| 克东县| 通辽市| 黑水县| 河津市| 山丹县| 福安市| 阳曲县| 武义县| 海晏县| 汉寿县|