對(duì)于 @Component
注解在日常的工作中相信很多小伙伴都會(huì)使用到车伞,作為一種 Spring
容器托管的通用模式組件择懂,任何被 @Component
注解標(biāo)注的組件都會(huì)被 Spring
容器掃描。
那么有的小伙伴就要問了另玖,很多時(shí)候我們并沒有直接寫 @Component
注解呀困曙,寫的是類似于 @Service
,@RestController
谦去,@Configuration
等注解慷丽,不也是一樣可以被掃描到嗎?那這個(gè) @Component
有什么特別的嗎鳄哭?
元注解
在回答上面的問題之前要糊,我們先來(lái)了解一下什么叫元注解,所謂元注解就是指一個(gè)能聲明在其他注解上的注解妆丘,換句話說(shuō)就是如果一個(gè)注解被標(biāo)注在其他注解上锄俄,那么它就是元注解局劲。
要說(shuō)明的是這個(gè)元注解并不是 Spring
領(lǐng)域的東西, 而是 Java
領(lǐng)域的奶赠,像 Java
中的很多注解比如 @Document
鱼填,@Repeatable
,@Target
等都屬于元注解毅戈。
根據(jù)上面的解釋我們可以發(fā)現(xiàn)在 Spring
容器里 @Component
就是以元注解的形式存在苹丸,因?yàn)槲覀兛梢栽诤芏嗥渌⒔饫锩嬲业剿纳碛埃缦滤?/p>
@Component 的派生性
通過上面的內(nèi)容我們是不是可以猜測(cè)一下那就是 @Component
注解的特性被"繼承"下來(lái)了苇经?這就可以解釋為什么我們可以直接寫@Service
赘理,@RestController
注解也是可以被掃描到的。但是由于 Java
的注解是不支持繼承的塑陵,比如你想通過下面的方式來(lái)實(shí)現(xiàn)注解的繼承是不合法的感憾。
為了驗(yàn)證我們的猜想,可以通過跟蹤源代碼來(lái)驗(yàn)證一下令花,我們的目的是研究為什么不直接使用 @Component
注解也能被 Spring
掃描到,換句話說(shuō)就是使用 @Service
和 @RestController
的注解也能成為 Spring Bean
凉倚。
那我們很自然的就可以想到兼都,在掃描的時(shí)候一定是根據(jù)注解來(lái)進(jìn)行了判斷是否要初始化成 Spring Bean
的。我們只要找到了判斷條件就可以解決我們的疑惑了稽寒。
由于 SpringBoot
項(xiàng)目是通過 main
方法進(jìn)行啟動(dòng)的扮碧,調(diào)試起來(lái)還是很方便的,阿粉這邊準(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)越來(lái)越接近掃描相關(guān)的內(nèi)容了谅河。
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.Factory#create
context
上下文創(chuàng)建完成過后咱旱,接下來(lái)我們我們會(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è)方法就是返回合法的候選組件。說(shuō)明這個(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
祷杈。那么接下來(lái)問題就轉(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
來(lái)循環(huán)查詢?cè)⒔膺M(jìn)行賦值器紧,代碼如下所示容贝,重點(diǎn)是這一行 Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false);
private void addAllMappings(Class<? extends Annotation> annotationType,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
Deque<AnnotationTypeMapping> 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<AnnotationTypeMapping> 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ì)比較枯燥和繁瑣茂缚,最后我們來(lái)簡(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 和如何獲取元注解,只要我們一步步看下去就是可以找到的阶淘。
最后說(shuō)明一下衙吩,Spring Framework
每個(gè)版本的具體實(shí)現(xiàn)會(huì)有差異,阿粉使用的版本是 5.3.24
溪窒,所以如果小伙伴看到自己的代碼追蹤的效果跟阿粉的不一樣也不會(huì)奇怪坤塞,可能是因?yàn)榘姹静灰粯佣眩贿^本質(zhì)上都是一樣的澈蚌。
本文由mdnice多平臺(tái)發(fā)布