成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

springboot bean掃描路徑的實(shí)現(xiàn)

瀏覽:38日期:2023-03-27 11:43:44

1:默認(rèn)掃描啟動(dòng)類所在路徑下所有的bean

2:可以在啟動(dòng)類中添加注解,手動(dòng)指定掃描路徑:

@ComponentScan(basePackages = {'com.xxx.service1.*','com.xxx.service2.**'})

補(bǔ)充:SpringBoot 是如何通過 @SpringBootApplication 掃描項(xiàng)目中的 Bean

原因

首先因?yàn)?XXXXXXXApplication 附帶 @SpringBootApplication 注解,而 @SpringBootApplication 注解的層次如下:

SpringBootApplication----@Inherited----@SpringBootConfiguration--------@Configuration----@EnableAutoConfiguration--------@Inherited--------@AutoConfigurationPackage------------@Inherited------------@Import(AutoConfigurationPackages.Registrar.class)--------@Import(AutoConfigurationImportSelector.class)----@ComponentScan--------@Repeatable(ComponentScans.class)實(shí)現(xiàn)

可以看到 @SpringBootApplication 繼承 @ComponentScan 與 @Configuration 用處如下;

掃描方法開始流程:

springboot bean掃描路徑的實(shí)現(xiàn)

主要觀察黃色方塊的方法,是具體掃描路徑的地方,具體實(shí)現(xiàn)流程如下:

springboot bean掃描路徑的實(shí)現(xiàn)

獲取 File 目錄下的所有以 class 為結(jié)尾的文件后,掃描工作就完成了, 剩下的就是 spring 判斷是否要管理此類的邏輯(例如:該類是否存在 @Component )

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Spring
相關(guān)文章: