Spring源碼分析之包掃描

一.基本知識點(diǎn)

1.Metadata 元數(shù)據(jù)

基本概念:

定義:元數(shù)據(jù)Metadata)侈咕,又稱中介數(shù)據(jù)公罕、中繼數(shù)據(jù),為描述數(shù)據(jù)的數(shù)據(jù),主要是描述數(shù)據(jù)屬性的信息耀销。

3699854-40620b7696251879.png

作用:

就是獲取類的相關(guān)屬性和信息

獲取方法:( AnnotationMetadata)

MetadataReader metadataReader = new SimpleMetadataReaderFactory().getMetadataReader(SpringbootApplication.class.getName());
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
底層獲取原理(通過asm獲取類的各個相關(guān)信息)(getMetadataReader)
// 封裝成資源文件
Resource resource = new DefaultResourceLoader().getResource("classpath:com/example/springboot/SpringbootApplication.class");
InputStream is = new BufferedInputStream(resource.getInputStream());
// org.springframework.asm.ClassReader  獲取類的相關(guān)信息
        ClassReader classReader = new ClassReader(resource.getInputStream());
        AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(SpringbootApplication.class.getClassLoader());
// AnnotationMetadataReadingVisitor(可看作是AnnotationMetadata)根據(jù)類的相關(guān)信息封裝成Spring所需的類的相關(guān)信息
        classReader.accept(visitor, ClassReader.SKIP_DEBUG);

示例

1548996754837.png

?

2.源碼分析

核心方法:findCandidateComponents

ClassPathScanningCandidateComponentProvider.findCandidateComponents(String basePackage)

代碼:

ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
        Set<BeanDefinition> candidateComponents = provider.findCandidateComponents("com.example.springboot");

返回結(jié)果:


1548997224165.png

findCandidateComponents方法:

// spring5.0開始 索引 開啟的話生成文件META-INF/spring.components 后面加載直接從本地文件讀嚷ゾ臁(一般不建議開啟 spring.index.ignore=true)
if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
    return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
}
else {
    return scanCandidateComponents(basePackage);
}

scanCandidateComponents方法

// 獲取包類的所有資源文件
Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);

for (Resource resource : resources) {
    // 相當(dāng)于獲取(AnnotationMetadata)
    MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
    //1.重點(diǎn)
    if (isCandidateComponent(metadataReader)) {
        ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
        sbd.setResource(resource);
        sbd.setSource(resource);
         //2.重點(diǎn)
        if (isCandidateComponent(sbd)) {
            if (debugEnabled) {
                logger.debug("Identified candidate component class: " + resource);
            }
            candidates.add(sbd);
        }
    }   
}

isCandidateComponent方法

protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
        // 默認(rèn)為空
        for (TypeFilter tf : this.excludeFilters) {
            if (tf.match(metadataReader, getMetadataReaderFactory())) {
                return false;
            }
        }
    
        // 默認(rèn)有(見registerDefaultFilters方法)
        //(new AnnotationTypeFilter(Component.class))
        //new AnnotationTypeFilter(javax.inject.Named)
        for (TypeFilter tf : this.includeFilters) {
            if (tf.match(metadataReader, getMetadataReaderFactory())) {
                return isConditionMatch(metadataReader);
            }
        }
        return false;
    }

match方法

if (matchSelf(metadataReader)) {
    return true;
}

protected boolean matchSelf(MetadataReader metadataReader) {
    // 獲取元注解
    AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
    // AnnortationMetadata(當(dāng)前類的注解)(annotationSet )
    return metadata.hasAnnotation(this.annotationType.getName()) ||
        // metaAnnotationMap(當(dāng)前類的注解以及當(dāng)前類的注解的派生注解)
        (this.considerMetaAnnotations && metadata.hasMetaAnnotation(this.annotationType.getName()));
}

metadata.hasAnnotation:例如如果當(dāng)前類上的注解為@Service,那么元注解的annotationSet屬性則為Service熊尉,不包含默認(rèn)過濾器中的@Component和@named條件之一罐柳,返回false。
metadata.hasMetaAnnotation:例如如果當(dāng)前類上的注解為@Service狰住,那么元注解的metaAnotationMap則有@Service和@Component(@Service是@Component的派生屬性)肮蛹,包含默認(rèn)過濾器的@Component條件,則為true。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末伦忠,一起剝皮案震驚了整個濱河市昆码,隨后出現(xiàn)的幾起案子赋咽,更是在濱河造成了極大的恐慌舌菜,老刑警劉巖日月,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绊起,死亡現(xiàn)場離奇詭異虱歪,居然都是意外死亡师枣,警方通過查閱死者的電腦和手機(jī)践美,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門许布,熙熙樓的掌柜王于貴愁眉苦臉地迎上來杂曲,“玉大人解阅,你說我怎么就攤上這事货抄。” “怎么了怪与?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我括授,道長荚虚,這世上最難降的妖魔是什么院水? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任恢恼,我火速辦了婚禮漏隐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好王暗,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著隐绵,像睡著了一般依许。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上膘婶,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天拯刁,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的乏悄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼赴邻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起姥敛,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎揍拆,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贮喧,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡箱沦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年谓形,在試婚紗的時候發(fā)現(xiàn)自己被綠了寒跳。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片竹椒。...
    茶點(diǎn)故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖胸完,靈堂內(nèi)的尸體忽然破棺而出书释,到底是詐尸還是另有隱情,我是刑警寧澤爆惧,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站扯再,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏址遇。R本人自食惡果不足惜叔收,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一傲隶、第九天 我趴在偏房一處隱蔽的房頂上張望饺律。 院中可真熱鬧,春花似錦跺株、人聲如沸复濒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽砸泛。三九已至十籍,卻和暖如春盏筐,著一層夾襖步出監(jiān)牢的瞬間围俘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工漾抬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宿亡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓纳令,卻偏偏與公主長得像她混,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子泊碑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容