今天给大家分享Spring中@ComponentScan注解的系列用法,希望对大家能有所帮助!
@ComponentScan注解一般和@Configuration注解一起使用,主要的介绍作用就是定义包扫描的规则,然后根据定义的系列规则找出哪些需类需要自动装配到spring的bean容器中,然后交由spring进行统一管理。注解说明:针对标注了@Controller、用法@Service、介绍@Repository、系列@Component 的注解类都可以别spring扫描到。香港云服务器

指定要扫描的介绍包路径
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.javapackage 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资讯网服务器租用亿华云IT技术网香港云服务器企商汇益华科技益强智囊团汇智坊运维纵横技术快报益强编程堂亿华互联云智核极客码头益强数据堂益强科技亿华云计算益华科技科技前瞻全栈开发思维库亿华科技益强智未来云站无忧亿华智造益华IT技术论坛益强IT技术网益强前沿资讯多维IT资讯编程之道益强科技益强编程舍极客编程码上建站码力社益强资讯优选亿华智慧云智能时代亿华云创站工坊IT资讯网
0.3381s , 17455.5390625 kb
Copyright © 2025 Powered by Spring系列之@ComponentScan注解用法介绍,亿华互联 滇ICP备2023000592号-16