Spring系列之@ComponentScan注解用法介绍

开发 架构
今天给大家分享Spring中@ComponentScan注解的用法,希望对大家能有所帮助!

今天给大家分享Spring中@ComponentScan注解的用法,希望对大家能有所帮助!

1、@ComponentScan注解的作用

@ComponentScan注解一般和@Configuration注解一起使用,主要的作用就是定义包扫描的规则,然后根据定义的规则找出哪些需类需要自动装配到spring的bean容器中,然后交由spring进行统一管理。说明:针对标注了@Controller、@Service、@Repository、@Component 的类都可以别spring扫描到。

2、@ComponentScan注解属性介绍

2.1 value

指定要扫描的包路径

2.2 excludeFilters(排除规则)

excludeFilters=Filter[] 指定包扫描的时候根据规则指定要排除的组件

2.3 includeFilters(包含规则)

includeFilters =Filter[] 指定包扫描的时候根据规则指定要包含的组件.注意:要设置useDefaultFilters = false(系统默认为true,需要手动设置) includeFilters包含过滤规则才会生效。

2.4 FilterType属性

FilterType.ANNOTATION:按照注解过滤FilterType.ASSIGNABLE_TYPE:按照给定的类型,指定具体的类,子类也会被扫描到FilterType.ASPECTJ:使用ASPECTJ表达式FilterType.REGEX:正则FilterType.CUSTOM:自定义规则useDefaultFilters: 配置是否开启可以对加@Component,@Repository,@Service,@Controller注解的类进行检测, 针对Java8 语法可以指定多个@ComponentScan,Java8以下可以用 @ComponentScans() 配置多个规则

3、示例

3.1 各种过滤过滤规则示例



// 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}
),

})

3.2 自定义过滤规则 需要新建 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);
}
}
}

具体的运行效果可以查看控制台输出结果,是否和预期的一样,具体有不清楚的欢迎沟通交流。

责任编辑:姜华 来源: IT技术分享社区
相关推荐

2022-02-19 07:41:36

Bean注解项目

2022-03-03 07:34:31

注解容器作用域

2022-12-19 08:14:30

注解开发配置

2021-09-16 10:05:09

鸿蒙HarmonyOS应用

2023-08-04 08:25:03

客户配置Spring

2021-07-09 07:52:34

SpringContextEventListen

2024-02-23 10:33:34

SpringBean容器

2016-09-21 14:17:11

JUnit架构API

2016-09-23 10:34:32

JUnitGradleAgile

2022-06-09 07:27:14

JavaSpring容器

2022-05-30 11:17:44

Spring容器配置

2024-01-03 07:57:11

高级参数PowerShellVerbose 参数

2023-09-21 07:06:17

PSDriveProvider

2021-05-07 21:32:51

SpringIOC分析

2021-05-06 18:17:52

SpringAOP理解

2016-09-23 10:20:22

JUnit扩展模型Extension

2020-11-05 11:30:46

PythonNumPy数组

2023-08-26 11:36:31

Java框架Spring

2023-06-02 16:24:46

SpringBootSSM

2017-04-24 12:07:44

Spark大数据并行计算
点赞
收藏

51CTO技术栈公众号