Spring的@ComponentScan注解用法介紹
1、@ComponentScan注解的作用
@ComponentScan注解一般和@Configuration注解一起使用,主要的作用就是定義包掃描的規(guī)則,然后根據(jù)定義的規(guī)則找出哪些需類需要自動(dòng)裝配到spring的bean容器中,然后交由spring進(jìn)行統(tǒng)一管理。
說(shuō)明:針對(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ò)濾規(guī)則才會(huì)生效。
4 FilterType屬性
- FilterType.ANNOTATION:按照注解過(guò)濾
- FilterType.ASSIGNABLE_TYPE:按照給定的類型,指定具體的類,子類也會(huì)被掃描到
- FilterType.ASPECTJ:使用ASPECTJ表達(dá)式
- FilterType.REGEX:正則
- FilterType.CUSTOM:自定義規(guī)則
useDefaultFilters: 配置是否開(kāi)啟可以對(duì)加@Component,@Repository,@Service,@Controller注解的類進(jìn)行檢測(cè), 針對(duì)Java8 語(yǔ)法可以指定多個(gè)@ComponentScan,Java8以下可以用 @ComponentScans() 配置多個(gè)規(guī)則
3、示例
1 各種過(guò)濾過(guò)濾規(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ò)濾規(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注解過(guò)濾排除不加載某個(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)文章
Java BeanMap實(shí)現(xiàn)Bean與Map的相互轉(zhuǎn)換
這篇文章主要介紹了利用BeanMap進(jìn)行對(duì)象與Map的相互轉(zhuǎn)換,通過(guò)net.sf.cglib.beans.BeanMap類中的方法來(lái)轉(zhuǎn)換,效率極高,本文給大家分享實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2022-11-11
ssm項(xiàng)目實(shí)現(xiàn)用戶登陸持久化(token)
這篇文章主要介紹了ssm項(xiàng)目實(shí)現(xiàn)用戶登陸持久化(token),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java農(nóng)夫過(guò)河問(wèn)題的繼承與多態(tài)實(shí)現(xiàn)詳解
這篇文章主要介紹了Java農(nóng)夫過(guò)河問(wèn)題的繼承與多態(tài)實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解
這篇文章主要介紹了java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的示例代碼
這篇文章將通過(guò)Java實(shí)現(xiàn)一個(gè)簡(jiǎn)答的圖書(shū)管理系統(tǒng),本圖書(shū)管理系統(tǒng)用對(duì)象數(shù)組的方式來(lái)提供操作方法,比較特別,建議新手學(xué)習(xí),這對(duì)理解Java面向?qū)ο笥泻艽髱椭?/div> 2022-11-11
JavaMe開(kāi)發(fā)自適應(yīng)滾動(dòng)顯示
我們??吹揭恍L動(dòng)顯示的實(shí)例,比如UC瀏覽器中,顯示網(wǎng)頁(yè)的內(nèi)容。當(dāng)內(nèi)容比較多時(shí),采用滾動(dòng)分頁(yè)顯示是合理的。在Canvas中繪圖中,多余的內(nèi)容被截?cái)嗔恕H绾螌?shí)現(xiàn)滾動(dòng)分頁(yè)顯示呢?2015-09-09最新評(píng)論

