SpringBoot默認包掃描機制及@ComponentScan指定掃描路徑詳解
SpringBoot默認包掃描機制
標注了@Component和@Component的衍生注解如@Controller,@Service,@Repository就可以把當前的Bean加入到IOC容器中。那么SpringBoot是如何知道要去掃描@Component注解的呢。@ComponentScan做的事情就是告訴Spring從哪里找到bean
SpringBoot默認包掃描機制: 從啟動類所在包開始,掃描當前包及其子級包下的所有文件。我們可以通過以下的測試來驗證一下。
啟動應(yīng)用并訪問BannerController這個控制器,目錄結(jié)構(gòu)如圖

訪問結(jié)果正常

當把BannerController移動到上一級目錄,應(yīng)用可以正常啟動

但是再次訪問剛才的路徑時卻出現(xiàn)了如下錯誤,代碼是沒有變動的,是Controller掃描 不到了。

實際上SpringBoot是通過@ComponentScan進行掃描。默認情況下,入口類上面的@SpringBootApplication里面有一個@ComponentScan,也就相當于@ComponentScan標注在入口類上。
所以默認情況下,掃描入口類同級及其子級包下的所有文件。當我們想自己制定包掃描路徑就需要加一個@ComponentScan
@ComponentScan的使用
常用參數(shù)含義
basePackages與value: 用于指定包的路徑,進行掃描(默認參數(shù))basePackageClasses: 用于指定某個類的包的路徑進行掃描includeFilters: 包含的過濾條件FilterType.ANNOTATION:按照注解過濾FilterType.ASSIGNABLE_TYPE:按照給定的類型FilterType.ASPECTJ:使用ASPECTJ表達式FilterType.REGEX:正則FilterType.CUSTOM:自定義規(guī)則excludeFilters: 排除的過濾條件,用法和includeFilters一樣nameGenerator: bean的名稱的生成器useDefaultFilters: 是否開啟對@Component,@Repository,@Service,@Controller的類進行檢測
指定要掃描的包
上述例子,如果想掃描啟動類上一級包,使用@ComponentScan指定包掃描路徑,即可將BannerController加入到容器
@SpringBootApplication
@ComponentScan("com.lin")
public class MissyouApplication {
public static void main(String[] args) {
SpringApplication.run(MissyouApplication.class, args);
}
}
excludeFilters 排除某些包的掃描
測試類準備:
@Controller
public class BannerController {
BannerController(){
System.out.println("Hello BannerController");
}
}
--------------------------------------------------------------------
@Service
public class TestService {
TestService(){
System.out.println("Hello TestService");
}
}
目錄結(jié)構(gòu)如下:

啟動類上加@ComponentScan指定掃描lin這個包并排除@Controller這個注解標注的類
@SpringBootApplication
@ComponentScan(value = "com.lin",
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class})})
public class MissyouApplication {
public static void main(String[] args) {
SpringApplication.run(MissyouApplication.class, args);
}
}
啟動應(yīng)用,控制臺打印出了TestService而沒有BannerController

@Component與@ComponentScan
在某個類上使用@Component注解,表明當需要創(chuàng)建類時,這個被注解標注的類是一個候選類。就像是有同學(xué)在舉手。 @ComponentScan 用于掃描指定包下的類。就像看都有哪些舉手了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis limit分頁設(shè)置的實現(xiàn)
這篇文章主要介紹了MyBatis limit分頁設(shè)置的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
前端dist包放到后端springboot項目下一起打包圖文教程
這篇文章主要介紹了前端dist包放到后端springboot項目下一起打包的相關(guān)資料,具體步驟包括前端打包、將前端文件復(fù)制到后端項目的static目錄、后端打包、驗證部署成功等,需要的朋友可以參考下2025-01-01
springboot 如何重定向redirect 并隱藏參數(shù)
這篇文章主要介紹了springboot 如何重定向redirect 并隱藏參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
MPAndroidChart開源圖表庫的使用介紹之餅狀圖、折線圖和柱狀圖
這篇文章主要介紹了MPAndroidChart開源圖表庫的使用介紹之餅狀圖、折線圖和柱狀圖的相關(guān)資料,需要的朋友可以參考下2016-02-02
Spring IoC學(xué)習(xí)之ApplicationContext中refresh過程詳解
這篇文章主要給大家介紹了關(guān)于Spring IoC學(xué)習(xí)之ApplicationContext中refresh過程的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

