Spring的@ComponentScan注解用法介紹
1、@ComponentScan注解的作用
@ComponentScan注解一般和@Configuration注解一起使用,主要的作用就是定義包掃描的規(guī)則,然后根據(jù)定義的規(guī)則找出哪些需類需要自動(dòng)裝配到spring的bean容器中,然后交由spring進(jìn)行統(tǒng)一管理。
說明:針對(duì)標(biāo)注了@Controller、@Service、@Repository、@Component 的類都可以別spring掃描到。
2、@ComponentScan注解屬性介紹
1 value
指定要掃描的包路徑
2 excludeFilters(排除規(guī)則)
excludeFilters=Filter[] 指定包掃描的時(shí)候根據(jù)規(guī)則指定要排除的組件
3 includeFilters(包含規(guī)則)
includeFilters =Filter[] 指定包掃描的時(shí)候根據(jù)規(guī)則指定要包含的組件.
注意:要設(shè)置useDefaultFilters = false(系統(tǒng)默認(rèn)為true,需要手動(dòng)設(shè)置) includeFilters包含過濾規(guī)則才會(huì)生效。
4 FilterType屬性
- FilterType.ANNOTATION:按照注解過濾
- FilterType.ASSIGNABLE_TYPE:按照給定的類型,指定具體的類,子類也會(huì)被掃描到
- FilterType.ASPECTJ:使用ASPECTJ表達(dá)式
- FilterType.REGEX:正則
- FilterType.CUSTOM:自定義規(guī)則
useDefaultFilters: 配置是否開啟可以對(duì)加@Component,@Repository,@Service,@Controller注解的類進(jìn)行檢測(cè), 針對(duì)Java8 語(yǔ)法可以指定多個(gè)@ComponentScan,Java8以下可以用 @ComponentScans() 配置多個(gè)規(guī)則
3、示例
1 各種過濾過濾規(guī)則示例
// includeFilters 用法 包含Animal.class類可以被掃描到,包括其子類
@ComponentScan(value = "com.spring"
includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {Animal.class}
)}
)
// excludeFilters 用法 排除包含@Controller注解的類
@ComponentScan(value = "com.spring"
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class}
),
})
// ComponentScans用法
@ComponentScans(
value = {
@ComponentScan(value = "com.spring"
, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class}
)
}, useDefaultFilters = false) ,
@ComponentScan(value = "com.spring"
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = { Repository.class}
)
})
}
)*/
// @ComponentScan
// 針對(duì)Java8 語(yǔ)法可以指定多個(gè)@ComponentScan,Java8以下可以用 //@ComponentScans() 配置多個(gè)規(guī)則
@ComponentScan(value = "com.spring"
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class, Controller.class}
),
}, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class, Controller.class}
),
})2 自定義過濾規(guī)則 需要新建 TestTypeFilter.java
package com.spring.config;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import java.io.IOException;
/**
* metadataReader 讀取到當(dāng)前正在掃描的類信息
* metadataReaderFactory 可以獲取到其他任何類的信息
*/
public class TestTypeFilter implements TypeFilter {
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//獲取當(dāng)前類注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
// 獲取當(dāng)前正在掃描的類信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 獲取當(dāng)前類資源信息(比如類的文件路徑)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
System.out.println("類名:" + className);
if (className.contains("controller")) {
return true;
} else {
return false;
}
}
}3 新建測(cè)試類 TestComponentScan.java
package com.spring.test;
import com.spring.config.TestComponentScanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestComponentScan {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestComponentScanConfig.class);
String[] names = annotationContext.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}具體的運(yùn)行效果可以查看控制臺(tái)輸出結(jié)果,是否和預(yù)期的一樣,具體有不清楚的歡迎溝通交流。
到此這篇關(guān)于Spring的@ComponentScan注解用法介紹的文章就介紹到這了,更多相關(guān)@ComponentScan注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@ComponentScan注解過濾排除不加載某個(gè)類的3種方法
- spring?boot自動(dòng)裝配之@ComponentScan注解用法詳解
- spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則
- Spring @ComponentScan注解使用案例詳細(xì)講解
- Spring @ComponentScan注解掃描組件原理
- Spring?component-scan?XML配置與@ComponentScan注解配置
- 詳解Spring系列之@ComponentScan自動(dòng)掃描組件
- 詳解Spring系列之@ComponentScan批量注冊(cè)bean
相關(guān)文章
javaweb實(shí)戰(zhàn)之商城項(xiàng)目開發(fā)(二)
這篇文章主要針對(duì)javaweb商城項(xiàng)目開發(fā)進(jìn)行實(shí)戰(zhàn)演習(xí),利用mybatis創(chuàng)建DAO層,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Java中如何實(shí)現(xiàn)類的熱加載和熱部署詳解
在應(yīng)用運(yùn)行的時(shí)升級(jí)軟件,無需重新啟動(dòng)的方式有兩種,熱部署和熱加載,這篇文章主要介紹了Java中如何實(shí)現(xiàn)類的熱加載和熱部署的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-05-05
SpringCloudGateway?自定義局部過濾器場(chǎng)景分析
這篇文章主要介紹了SpringCloudGateway?自定義局部過濾器場(chǎng)景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-06-06
SpringBoot自定義注解API數(shù)據(jù)加密和簽名校驗(yàn)
這篇文章主要介紹了SpringBoot自定義注解API數(shù)據(jù)加密和簽名校驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
深入理解Netty?FastThreadLocal優(yōu)缺點(diǎn)及實(shí)現(xiàn)邏輯
本文以線上詭異問題為切入點(diǎn),通過對(duì)比JDK ThreadLocal和Netty FastThreadLocal實(shí)現(xiàn)邏輯以及優(yōu)缺點(diǎn),并深入解讀源碼,由淺入深理解Netty FastThreadLocal2023-10-10
關(guān)于Mybatis的@param注解及多個(gè)傳參
這篇文章主要介紹了關(guān)于Mybatis的@param注解及多個(gè)傳參,@Param的作用就是給參數(shù)命名,比如在mapper里面某方法A(int id),當(dāng)添加注解后A(@Param(“userId”) int id),也就是說外部想要取出傳入的id值,只需要取它的參數(shù)名userId就可以了,需要的朋友可以參考下2023-05-05

