日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
你知道什么是@Component注解的派生性嗎?

對(duì)于 @Component? 注解在日常的工作中相信很多小伙伴都會(huì)使用到,作為一種 Spring? 容器托管的通用模式組件,任何被 @Component? 注解標(biāo)注的組件都會(huì)被 Spring 容器掃描。

目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、西峽網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

那么有的小伙伴就要問了,很多時(shí)候我們并沒有直接寫 @Component? 注解呀,寫的是類似于 @Service,@RestController,@Configuration? 等注解,不也是一樣可以被掃描到嗎?那這個(gè) @Component 有什么特別的嗎?

元注解

在回答上面的問題之前,我們先來了解一下什么叫元注解,所謂元注解就是指一個(gè)能聲明在其他注解上的注解,換句話說就是如果一個(gè)注解被標(biāo)注在其他注解上,那么它就是元注解。

要說明的是這個(gè)元注解并不是 Spring? 領(lǐng)域的東西, 而是 Java? 領(lǐng)域的,像 Java? 中的很多注解比如 @Document,@Repeatable? ,@Target 等都屬于元注解。

根據(jù)上面的解釋我們可以發(fā)現(xiàn)在 Spring? 容器里 @Component 

Configuration

controller

@Component 的派生性

通過上面的內(nèi)容我們是不是可以猜測(cè)一下那就是 @Component? 注解的特性被"繼承"下來了?這就可以解釋為什么我們可以直接寫@Service,@RestController? 注解也是可以被掃描到的。但是由于 Java 的注解是不支持繼承的,比如你想通過下面的方式來實(shí)現(xiàn)注解的繼承是不合法的。

@interface

為了驗(yàn)證我們的猜想,可以通過跟蹤源代碼來驗(yàn)證一下,我們的目的是研究為什么不直接使用 @Component? 注解也能被 Spring? 掃描到,換句話說就是使用 @Service? 和 @RestController? 的注解也能成為 Spring Bean。

那我們很自然的就可以想到,在掃描的時(shí)候一定是根據(jù)注解來進(jìn)行了判斷是否要初始化成 Spring Bean 的。我們只要找到了判斷條件就可以解決我們的疑惑了。

由于 SpringBoot? 項(xiàng)目是通過 main? 方法進(jìn)行啟動(dòng)的,調(diào)試起來還是很方便的,阿粉這邊準(zhǔn)備了一個(gè)簡(jiǎn)單的 SpringBoot? 工程,里面除了啟動(dòng)類之外只有一個(gè)DemoController.java 代碼如下

package com.example.demojar.controller;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
}

啟動(dòng)類如下

package com.example.demojar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication(scanBasePackages = {"com.example.demojar"})
public class DemoJarApplication {

public static void main(String[] args){
SpringApplication.run(DemoJarApplication.class, args);
}
}

Debug run? 方法,我們可以定位到 org.springframework.boot.SpringApplication#run(java.lang.String...) ?方法,該方法里面會(huì)初始化 SpringBoot? 上下文 context。

context = createApplicationContext();

默認(rèn)情況下會(huì)進(jìn)到下面的方法,并創(chuàng)建 AnnotationConfigServletWebServerApplicationContext? 并且其構(gòu)造函數(shù)中構(gòu)造了 ClassPathBeanDefinitionScanner 類路徑 Bean 掃描器。此處已經(jīng)越來越接近掃描相關(guān)的內(nèi)容了。

org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.Factory#create

context? 上下文創(chuàng)建完成過后,接下來我們我們會(huì)接入到 org.springframework.context.support.AbstractApplicationContext#refresh?,再到 org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors

org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry

org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions

經(jīng)過上面的步驟,最終可以可以定位到掃描的代碼在下面的方法 org.springframework.context.annotation.ComponentScanAnnotationParser#parse? 里面,調(diào)用前面上下文初始化的掃描器的 org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan 方法,

到這里我們已經(jīng)定位到了掃描具體包路徑的方法,這個(gè)方法里面主要看 findCandidateComponents(basePackage); 方法的內(nèi)容,這個(gè)方法就是返回合法的候選組件。說明這個(gè)方法會(huì)最終返回需要被注冊(cè)成 Spring Bean 的候選組件,那我們重點(diǎn)就要看這個(gè)方法的實(shí)現(xiàn)。

跟蹤這個(gè)方法 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#findCandidateComponents? 進(jìn)去我們可以看到通過加進(jìn)類路徑里面的資源文件,然后再根據(jù)資源文件生成 MetadataReader? 對(duì)象,最后判斷這個(gè) MetadataReader? 對(duì)象是否滿足候選組件的條件,如果滿足就添加到 Set 集合中進(jìn)行返回。

繼續(xù)追蹤源碼我們可以找到具體的判斷方法在 org.springframework.core.type.filter.AnnotationTypeFilter#matchSelf? 方法中,如下所示,可以看到這里對(duì) MetadataReader? 對(duì)象進(jìn)行了判斷是否有元注解 @Component?。在調(diào)試的時(shí)候我們會(huì)發(fā)現(xiàn) DemoController? 在此處會(huì)返回 true?,并且該 MetadataReader? 對(duì)象里面還有多個(gè) mappings? ,其實(shí)這些 mappings? 對(duì)應(yīng)的就是 Spring 的注解。

這個(gè) mappings? 里面的注解確實(shí)包含了 @Component? 注解,因此會(huì)返回 true?。那么接下來問題就轉(zhuǎn)換成,我們的 DemoController? 對(duì)應(yīng)的 MetadataReader 對(duì)象是如何創(chuàng)建的。

我們看回到 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#scanCandidateComponents? 方法,看看具體 MetadataReader 對(duì)象是如何創(chuàng)建的,MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);

通過構(gòu)造方法 org.springframework.core.type.classreading.SimpleMetadataReader#SimpleMetadataReader? 進(jìn)行 MetadataReader?  對(duì)象的創(chuàng)建,org.springframework.core.type.classreading.SimpleAnnotationMetadataReadingVisitor#visitEnd?,最終定位到 org.springframework.core.annotation.MergedAnnotationsCollection#MergedAnnotationsCollection? 這里進(jìn)行 mappings 賦值。

繼續(xù)定位到 org.springframework.core.annotation.AnnotationTypeMappings.Cache#createMappings,org.springframework.core.annotation.AnnotationTypeMappings#addAllMappings,addAllmappings? 方法,內(nèi)部使用了一個(gè) while? 循環(huán)和 Deque? 來循環(huán)查詢?cè)⒔膺M(jìn)行賦值,代碼如下所示,重點(diǎn)是這一行 Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false);

private void addAllMappings(Class annotationType,
Set> visitedAnnotationTypes){

Deque queue = new ArrayDeque<>();
addIfPossible(queue, null, annotationType, null, visitedAnnotationTypes);
while (!queue.isEmpty()) {
AnnotationTypeMapping mapping = queue.removeFirst();
this.mappings.add(mapping);
addMetaAnnotationsToQueue(queue, mapping);
}
}

private void addMetaAnnotationsToQueue(Deque queue, AnnotationTypeMapping source){
Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false);
for (Annotation metaAnnotation : metaAnnotations) {
if (!isMappable(source, metaAnnotation)) {
continue;
}
Annotation[] repeatedAnnotations = this.repeatableContainers.findRepeatedAnnotations(metaAnnotation);
if (repeatedAnnotations != null) {
for (Annotation repeatedAnnotation : repeatedAnnotations) {
if (!isMappable(source, repeatedAnnotation)) {
continue;
}
addIfPossible(queue, source, repeatedAnnotation);
}
}
else {
addIfPossible(queue, source, metaAnnotation);
}
}
}

綜上所述我們可以發(fā)現(xiàn)盡管我們沒有直接寫 @Component? 注解,只要我們加了類似于 @Service,@RestController? 等注解也是可以成功被 Spring? 掃描到注冊(cè)成 Spring Bean? 的,本質(zhì)的原因是因?yàn)檫@些注解底層都使用了 @Component? 作為元注解,經(jīng)過源碼分析我們發(fā)現(xiàn)了只要有 @Component 元注解標(biāo)注的注解類也是同樣會(huì)被進(jìn)行掃描的。

總結(jié)

上面的源碼追蹤過程可能會(huì)比較枯燥和繁瑣,最后我們來簡(jiǎn)單總結(jié)一下上面的內(nèi)容:

  • 方法org.springframework.boot.SpringApplication#run(java.lang.String...) ?中進(jìn)行 Spring 上下文的創(chuàng)建;
  • 在初始化上下文的時(shí)候會(huì)創(chuàng)建掃描器ClassPathBeanDefinitionScanner;
  • 在org.springframework.context.support.AbstractApplicationContext#refresh? 進(jìn)行 beanFactory 準(zhǔn)備;
  • org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan 進(jìn)行資源掃描
  • 在org.springframework.core.annotation.MergedAnnotationsCollection#MergedAnnotationsCollection? 進(jìn)行注解 mappings 的賦值;
  • org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#scanCandidateComponents 方法中進(jìn)行候選組件的判斷;

上面追蹤的過程可能會(huì)比較復(fù)雜,但是只要我們理解了原理還是可以慢慢跟上的,因?yàn)槲覀冎灰盐蘸昧朔较颍朗紫瓤隙〞?huì)進(jìn)行資源掃描,掃描完了肯定是根據(jù)注解之間的關(guān)系進(jìn)行判斷,最終得到我們需要的候選組件集合。至于如何創(chuàng)建 MetadataReader 和如何獲取元注解,只要我們一步步看下去就是可以找到的。

最后說明一下,Spring Framework? 每個(gè)版本的具體實(shí)現(xiàn)會(huì)有差異,阿粉使用的版本是 5.3.24 ,所以如果小伙伴看到自己的代碼追蹤的效果跟阿粉的不一樣也不會(huì)奇怪,可能是因?yàn)榘姹静灰粯佣?,不過本質(zhì)上都是一樣的。


分享文章:你知道什么是@Component注解的派生性嗎?
分享鏈接:http://www.5511xx.com/article/cdsegjj.html