今天给大家分享Spring中@ComponentScan注解的用法,希望对大家能有所帮助!
@ComponentScan注解一般和@Configuration注解一起使用,主要的作用就是定义包扫描的规则,然后根据定义的规则找出哪些需类需要自动装配到spring的bean容器中,然后交由spring进行统一管理。说明:针对标注了@Controller、@Service、@Repository、@Component 的类都可以别spring扫描到。
指定要扫描的包路径
excludeFilters=Filter[] 指定包扫描的时候根据规则指定要排除的组件
includeFilters =Filter[] 指定包扫描的时候根据规则指定要包含的组件.注意:要设置useDefaultFilters = false(系统默认为true,需要手动设置) includeFilters包含过滤规则才会生效。
FilterType.ANNOTATION:按照注解过滤FilterType.ASSIGNABLE_TYPE:按照给定的类型,指定具体的类,子类也会被扫描到FilterType.ASPECTJ:使用ASPECTJ表达式FilterType.REGEX:正则FilterType.CUSTOM:自定义规则useDefaultFilters: 配置是否开启可以对加@Component,@Repository,@Service,@Controller注解的类进行检测, 针对Java8 语法可以指定多个@ComponentScan,Java8以下可以用 @ComponentScans() 配置多个规则
复制
// 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 // 针对Java8 语法可以指定多个@ComponentScan,Java8以下可以用 //@ComponentScans() 配置多个规则@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} ), })
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
复制
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; } } }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
复制
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); } } }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
具体的运行效果可以查看控制台输出结果,是否和预期的一样,具体有不清楚的欢迎沟通交流。