Spring系列之@ComponentScan注解用法介紹
今天給大家分享Spring中@ComponentScan注解的用法,希望對大家能有所幫助!
1、@ComponentScan注解的作用
@ComponentScan注解一般和@Configuration注解一起使用,主要的作用就是定義包掃描的規(guī)則,然后根據(jù)定義的規(guī)則找出哪些需類需要自動裝配到spring的bean容器中,然后交由spring進行統(tǒng)一管理。說明:針對標注了@Controller、@Service、@Repository、@Component 的類都可以別spring掃描到。
2、@ComponentScan注解屬性介紹
2.1 value
指定要掃描的包路徑
2.2 excludeFilters(排除規(guī)則)
excludeFilters=Filter[] 指定包掃描的時候根據(jù)規(guī)則指定要排除的組件
2.3 includeFilters(包含規(guī)則)
includeFilters =Filter[] 指定包掃描的時候根據(jù)規(guī)則指定要包含的組件.注意:要設(shè)置useDefaultFilters = false(系統(tǒng)默認為true,需要手動設(shè)置) includeFilters包含過濾規(guī)則才會生效。
2.4 FilterType屬性
FilterType.ANNOTATION:按照注解過濾FilterType.ASSIGNABLE_TYPE:按照給定的類型,指定具體的類,子類也會被掃描到FilterType.ASPECTJ:使用ASPECTJ表達式FilterType.REGEX:正則FilterType.CUSTOM:自定義規(guī)則useDefaultFilters: 配置是否開啟可以對加@Component,@Repository,@Service,@Controller注解的類進行檢測, 針對Java8 語法可以指定多個@ComponentScan,Java8以下可以用 @ComponentScans() 配置多個規(guī)則
3、示例
3.1 各種過濾過濾規(guī)則示例
// includeFilters 用法 包含Animal.class類可以被掃描到,包括其子類
(value = "com.spring"
includeFilters = { .Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {Animal.class}
)}
)
// excludeFilters 用法 排除包含@Controller注解的類
(value = "com.spring"
, excludeFilters = {
.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class}
),
})
// ComponentScans用法
(
value = {
(value = "com.spring"
, includeFilters = {
.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class}
)
}, useDefaultFilters = false) ,
(value = "com.spring"
, excludeFilters = {
.Filter(type = FilterType.ANNOTATION
, classes = { Repository.class}
)
})
}
)*/
// @ComponentScan
// 針對Java8 語法可以指定多個@ComponentScan,Java8以下可以用 //@ComponentScans() 配置多個規(guī)則
(value = "com.spring"
, excludeFilters = {
.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class, Controller.class}
),
}, includeFilters = {
.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class, Controller.class}
),
})
3.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 讀取到當前正在掃描的類信息
* metadataReaderFactory 可以獲取到其他任何類的信息
*/
public class TestTypeFilter implements TypeFilter {
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//獲取當前類注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
// 獲取當前正在掃描的類信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 獲取當前類資源信息(比如類的文件路徑)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
System.out.println("類名:" + className);
if (className.contains("controller")) {
return true;
} else {
return false;
}
}
}
3.3 新建測試類 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);
}
}
}
具體的運行效果可以查看控制臺輸出結(jié)果,是否和預(yù)期的一樣,具體有不清楚的歡迎溝通交流。