SpringBoot詳細探究講解默認組件掃描
參考視頻:https://www.bilibili.com/video/BV1Bq4y1Q7GZ?p=6
通過視頻的學習和自身的理解整理出的筆記。
一、前期準備
1.1 創(chuàng)建工程
創(chuàng)建springboot項目,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)建一個簡單的Controller用于測試
@RestController public class HelloController { public void helloController() { System.out.println("創(chuàng)建了"); } @RequestMapping("hello") public String hello() { return "hello"; } }
二、探究過程
2.1 探究目標
在項目中我們創(chuàng)建了Controller,這個Controller是如何被spring自動加載的呢?為什么Controller必須放在啟動類的同級目錄下呢?
如果我們想要加載不在啟動類同級目錄下的bean對象,需要在啟動類中使用@ComponentScan
注解。
目標:SpringBoot項目中我們沒有設置組件掃描的包,為什么它會默認掃描啟動類目錄下所有的包。
2.2 探究過程
2.2.1 回顧容器bean的創(chuàng)建與刷新
在SpringApplication的run()
方法中,創(chuàng)建了spring容器context,并通過refreshContext(context)
更新容器加載我們自定義的bean對象。
我們發(fā)現(xiàn)在執(zhí)行完refreshContext(context)
代碼后,自定義的bean對象(HelloController)就已經(jīng)被創(chuàng)建了,說明refreshContext(context)
過程中創(chuàng)建了自定義bean對象。
下面我們看看究竟是refreshContext(context)
中哪些方法創(chuàng)建了自定義bean對象。
2.2.2 SpringApplication
我接著看refreshContext(context)
方法
?? refreshContext()
方法
?? refresh()
方法
2.2.3 ServletWebServerApplicationContext
再調用父類的refresh()方法
2.2.4 AbstractApplicationContext
?? refresh()
方法
在執(zhí)行完這行代碼后創(chuàng)建了自定義bean的beanDefination對象。下面來看看這行代碼。
?? invokeBeanFactoryPostProcessors()
方法
根據(jù)這個名字可以看出來是調用了bean工廠的后置處理器。
2.2.5 PostProcessorRegistrationDelegate
?? invokeBeanFactoryPostProcessors()
方法
調用bean工廠的后置處理器,這個方法很長,最終找到了是這行代碼,調用BeanDefinition注冊的后置處理。
?? invokeBeanDefinitionRegistryPostProcessors()
方法
拿到后置處理器,調用后置處理器的BeanDefinition注冊。
2.2.6 ConfigurationClassPostProcessor
?? postProcessBeanDefinitionRegistry()
方法
?? processConfigBeanDefinitions()
方法
把啟動類的beanDefinition對象添加到了configCandidates集合中,后面將要用到。
這行代碼執(zhí)行結束后就有了helloController。
這個parser是配置類的處理器,通過傳入很多參數(shù)構造了這個parser處理器。
parser.parse(candidates)
中,把啟動類對應的beanDefinitionHolder對象傳進去了。
下面看看這個parse
方法。
?? parse()
方法
2.2.7 ConfigurationClassParser
?? parse()
方法
?? processConfigurationClass()
方法
?? doProcessConfigurationClass()
方法
if (configClass.getMetadata().isAnnotated(Component.class.getName())) { ... }
判斷啟動類上是否加上了@Component
注解,這里的if條件成立。
因為@SpringBootApplication包含@SpringBootConfiguration,@SpringBootConfiguration包含@Configuration,@Configuration包含@Component,所以加上了@SpringBootApplication注解就相當于加上了@Component注解。
?? processMemberClasses()
方法
里面有很多處理各類注解的方法
// Process any @PropertySource annotations // Process any @ComponentScan annotations // Process any @Import annotations // Process any @ImportResource annotations // Process individual @Bean methods
后續(xù)將要對這個集合進行掃描,那么看看它是如何掃描的。
2.2.8 ComponentScanAnnotationParser
?? parse()
方法
ClassUtils.getPackageName(declaringClass)
:獲取啟動類所在的包,根據(jù)傳入類的全類名獲取包名。
scanner.doScan(StringUtils.toStringArray(basePackages))
:掃描啟動類所在的包
2.3 結論
在容器刷新時會調用BeanFactoryPostProcessor(Bean工廠后置處理器)進行處理。其中就有一個ConfigurationClassPostProcessor(配置類處理器)。在這個處理器中使用ConfigurationClassParser(配置類解析器)的parse方法去解析處理我們的配置類,其中就有對ComponentScan注解的解析處理。會去使用ComponentScanAnnotationParser的parse方法去解析。解析時如果發(fā)現(xiàn)沒有配置basePackage,它會去獲取我們加載了注解的這個類所在的包,作為我們的basepackage進行組件掃描。
到此這篇關于SpringBoot詳細探究講解默認組件掃描的文章就介紹到這了,更多相關SpringBoot組件掃描內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring?Lifecycle?和?SmartLifecycle區(qū)別面試精講
這篇文章主要為大家介紹了Spring?Lifecycle和SmartLifecycle的區(qū)別面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10IntelliJ IDEA搜索整個項目進行全局替換(有危險慎用)
今天小編就為大家分享一篇關于IntelliJ IDEA搜索整個項目進行全局替換(有危險慎用),小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10elasticsearch索引index數(shù)據(jù)功能源碼示例
這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04