springboot bean掃描路徑的實(shí)現(xiàn)
1:默認(rèn)掃描啟動(dòng)類所在路徑下所有的bean
2:可以在啟動(dòng)類中添加注解,手動(dòng)指定掃描路徑:
@ComponentScan(basePackages = {'com.xxx.service1.*','com.xxx.service2.**'})
補(bǔ)充:SpringBoot 是如何通過(guò) @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 用處如下;
掃描方法開始流程:
主要觀察黃色方塊的方法,是具體掃描路徑的地方,具體實(shí)現(xiàn)流程如下:
獲取 File 目錄下的所有以 class 為結(jié)尾的文件后,掃描工作就完成了, 剩下的就是 spring 判斷是否要管理此類的邏輯(例如:該類是否存在 @Component )
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 使用css實(shí)現(xiàn)全兼容tooltip提示框2. 關(guān)于Mysql-connector-java驅(qū)動(dòng)版本問(wèn)題總結(jié)3. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果4. 通過(guò)工廠模式返回Spring Bean方法解析5. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)6. python:刪除離群值操作(每一行為一類數(shù)據(jù))7. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)8. CSS自定義滾動(dòng)條樣式案例詳解9. python 批量下載bilibili視頻的gui程序10. Ajax提交post請(qǐng)求案例分析
