基于xml形式ComponentScan的使用一般如下
<context:component-scan base-package="com.luna" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
基于注解@ComponentScan的使用
@Configuration @ComponentScan(value = "com.luna", excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} ) }) public class ScanConfig { }
@ComponentScan一些常用参数
//基本属性 value/basePackages:指定一组要扫描的包,将扫描这些包及其子包下的文件.(默认基包为配置类所在的包)
classes:直接指定扫描的一些类,这些类所在的包及其子包也会被扫描。
nameGenerator:指定一个默认扫描组件的命名类,默认命名使用组件类名第一个字小写。
excludeFilters:需要一组@ComponentScan.Filter的注解配置,每个@Filter可以配置一组过滤规则,多个@Filter可以基于正则/注解/具体类配置不同的过滤规则。
includeFilters:反上。
FilterType.ANNOTATION(默认) | 一组注解,命中所有使用该注解的类,{MyAnno.class} | - |
FilterType.ASSIGNABLE_TYPE | 一组具体类 | - |
FilterType.ASPECTJ | - | 一组表达式,使用Aspectj表达式命中类 |
FilterType.REGEX | - | 一组表达式,使用正则命中类 |
FilterType.CUSTOM | 自定义的TypeFilter. | - |