SpringBoot詳細(xì)探究講解默認(rèn)組件掃描
參考視頻:https://www.bilibili.com/video/BV1Bq4y1Q7GZ?p=6
通過視頻的學(xué)習(xí)和自身的理解整理出的筆記。
一、前期準(zhǔn)備
1.1 創(chuàng)建工程
創(chuàng)建springboot項(xiàng)目,springboot版本為2.5.0,引入spring-boot-starter-web依賴,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>1.2 創(chuàng)建Controller
創(chuàng)建一個(gè)簡單的Controller用于測試
@RestController
public class HelloController {
public void helloController() {
System.out.println("創(chuàng)建了");
}
@RequestMapping("hello")
public String hello() {
return "hello";
}
}
二、探究過程
2.1 探究目標(biāo)
在項(xiàng)目中我們創(chuàng)建了Controller,這個(gè)Controller是如何被spring自動(dòng)加載的呢?為什么Controller必須放在啟動(dòng)類的同級(jí)目錄下呢?
如果我們想要加載不在啟動(dòng)類同級(jí)目錄下的bean對(duì)象,需要在啟動(dòng)類中使用@ComponentScan注解。
目標(biāo):SpringBoot項(xiàng)目中我們沒有設(shè)置組件掃描的包,為什么它會(huì)默認(rèn)掃描啟動(dòng)類目錄下所有的包。
2.2 探究過程
2.2.1 回顧容器bean的創(chuàng)建與刷新
在SpringApplication的run()方法中,創(chuàng)建了spring容器context,并通過refreshContext(context)更新容器加載我們自定義的bean對(duì)象。

我們發(fā)現(xiàn)在執(zhí)行完refreshContext(context)代碼后,自定義的bean對(duì)象(HelloController)就已經(jīng)被創(chuàng)建了,說明refreshContext(context)過程中創(chuàng)建了自定義bean對(duì)象。
下面我們看看究竟是refreshContext(context)中哪些方法創(chuàng)建了自定義bean對(duì)象。
2.2.2 SpringApplication
我接著看refreshContext(context)方法
?? refreshContext()方法

?? refresh()方法

2.2.3 ServletWebServerApplicationContext
再調(diào)用父類的refresh()方法

2.2.4 AbstractApplicationContext
?? refresh()方法

在執(zhí)行完這行代碼后創(chuàng)建了自定義bean的beanDefination對(duì)象。下面來看看這行代碼。
?? invokeBeanFactoryPostProcessors()方法
根據(jù)這個(gè)名字可以看出來是調(diào)用了bean工廠的后置處理器。

2.2.5 PostProcessorRegistrationDelegate
?? invokeBeanFactoryPostProcessors()方法
調(diào)用bean工廠的后置處理器,這個(gè)方法很長,最終找到了是這行代碼,調(diào)用BeanDefinition注冊的后置處理。

?? invokeBeanDefinitionRegistryPostProcessors()方法
拿到后置處理器,調(diào)用后置處理器的BeanDefinition注冊。

2.2.6 ConfigurationClassPostProcessor
?? postProcessBeanDefinitionRegistry()方法

?? processConfigBeanDefinitions()方法

把啟動(dòng)類的beanDefinition對(duì)象添加到了configCandidates集合中,后面將要用到。

這行代碼執(zhí)行結(jié)束后就有了helloController。
這個(gè)parser是配置類的處理器,通過傳入很多參數(shù)構(gòu)造了這個(gè)parser處理器。

parser.parse(candidates)中,把啟動(dòng)類對(duì)應(yīng)的beanDefinitionHolder對(duì)象傳進(jìn)去了。
下面看看這個(gè)parse方法。
?? parse()方法

2.2.7 ConfigurationClassParser
?? parse()方法

?? processConfigurationClass()方法

?? doProcessConfigurationClass()方法

if (configClass.getMetadata().isAnnotated(Component.class.getName())) { ... }
判斷啟動(dòng)類上是否加上了@Component注解,這里的if條件成立。
因?yàn)锧SpringBootApplication包含@SpringBootConfiguration,@SpringBootConfiguration包含@Configuration,@Configuration包含@Component,所以加上了@SpringBootApplication注解就相當(dāng)于加上了@Component注解。
?? processMemberClasses()方法
里面有很多處理各類注解的方法
// Process any @PropertySource annotations // Process any @ComponentScan annotations // Process any @Import annotations // Process any @ImportResource annotations // Process individual @Bean methods

后續(xù)將要對(duì)這個(gè)集合進(jìn)行掃描,那么看看它是如何掃描的。
2.2.8 ComponentScanAnnotationParser
?? parse()方法

ClassUtils.getPackageName(declaringClass):獲取啟動(dòng)類所在的包,根據(jù)傳入類的全類名獲取包名。
scanner.doScan(StringUtils.toStringArray(basePackages)):掃描啟動(dòng)類所在的包
2.3 結(jié)論
在容器刷新時(shí)會(huì)調(diào)用BeanFactoryPostProcessor(Bean工廠后置處理器)進(jìn)行處理。其中就有一個(gè)ConfigurationClassPostProcessor(配置類處理器)。在這個(gè)處理器中使用ConfigurationClassParser(配置類解析器)的parse方法去解析處理我們的配置類,其中就有對(duì)ComponentScan注解的解析處理。會(huì)去使用ComponentScanAnnotationParser的parse方法去解析。解析時(shí)如果發(fā)現(xiàn)沒有配置basePackage,它會(huì)去獲取我們加載了注解的這個(gè)類所在的包,作為我們的basepackage進(jìn)行組件掃描。
到此這篇關(guān)于SpringBoot詳細(xì)探究講解默認(rèn)組件掃描的文章就介紹到這了,更多相關(guān)SpringBoot組件掃描內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Lifecycle?和?SmartLifecycle區(qū)別面試精講
這篇文章主要為大家介紹了Spring?Lifecycle和SmartLifecycle的區(qū)別面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用
適配器模式的主要作用是在新接口和老接口之間進(jìn)行適配,通過將一個(gè)類的接口轉(zhuǎn)換成客戶期望的另一個(gè)接口,讓原本不兼容的接口可以合作無間,本文以實(shí)例解析Java設(shè)計(jì)模式編程中的適配器模式使用,需要的朋友可以參考下2016-05-05
IntelliJ IDEA搜索整個(gè)項(xiàng)目進(jìn)行全局替換(有危險(xiǎn)慎用)
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA搜索整個(gè)項(xiàng)目進(jìn)行全局替換(有危險(xiǎn)慎用),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
談?wù)凷pring AOP中@Aspect的高級(jí)用法示例
在Spring AOP中目前只有執(zhí)行方法這一個(gè)連接點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Spring AOP中@Aspect的高級(jí)用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
SpringBoot實(shí)現(xiàn)跨域的幾種常用方式總結(jié)
跨域是指一個(gè)域下的文檔或腳本試圖去請(qǐng)求另一個(gè)域下的資源,或者涉及到兩個(gè)不同域名的資源之間的交互,由于同源策略(Same Origin Policy)的限制,瀏覽器不允許跨域請(qǐng)求,本文小編給大家分享了SpringBoot實(shí)現(xiàn)跨域的幾種常用方式,需要的朋友可以參考下2023-09-09
基于Java中進(jìn)制的轉(zhuǎn)換函數(shù)詳解
下面小編就為大家?guī)硪黄贘ava中進(jìn)制的轉(zhuǎn)換函數(shù)詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
elasticsearch索引index數(shù)據(jù)功能源碼示例
這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04

