基于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", includeFilters = {
// @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
// @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} )
@ComponentScan.Filter(type = FilterType.CUSTOM,value = {MyFilter.class})
},useDefaultFilters = false)*/
@ComponentScan(value = "com.luna",
//排除某些类 excludeFilters = { //排除Controller注解标注的类 @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}), //排除指定类型的类 @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} ) }) public class ScanConfig { }
测试
@Test public void test(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ScanConfig.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); Arrays.asList(beanDefinitionNames).stream().forEach(x->System.out.println("扫描到的Bean---"+x)); }
@ComponentScan一些常用参数
//基本属性 value/basePackages:指定一组要扫描的包,将扫描这些包及其子包下的文件.(默认基包为配置类所在的包)
classes:直接指定扫描的一些类,这些类所在的包及其子包也会被扫描。
nameGenerator:指定一个默认扫描组件的命名类, 默认命名使用组件类名第一个字小写。
excludeFilters:指定扫描的时候排除某些类,按什么规则,需要一组@ComponentScan.Filter的注解配置,每个@Filter可以配置一组过滤规则,多个@Filter可以基于正则/注解/具体类配置不同的过滤规则。
includeFilters:指定扫描的时候,只包含某些类。
useDefaultFilters 自动扫描Controller Component Service Resposity 这四个注解
所以在不指定明确包的情况下如 com.luna 全包扫描,会扫描到Controller Component Service Resposity 四个注解
一、注解说明
@ComponentScan:会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类
它里面的属性: value指定扫描的包,includeFilters包含那些过滤,excludeFilters不包含那些过滤,useDefaultFilters默认的过滤规则是开启的,如果我们要自定义的话是要关闭的。其中@Filters是一个过滤器的接口。
@Filters 指过滤规则,FilterType指定过滤的规则(
FilterType.ANNOTATION:按照注解
FilterType.ASSIGNABLE_TYPE:按照给定的类型;
FilterType.ASPECTJ:使用ASPECTJ表达式
FilterType.REGEX:使用正则指定
FilterType.CUSTOM:使用自定义规则)
classes指定过滤的类
示列如下:
package com.guang.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import com.guang.entity.Person;
@Configuration
// @ComponentScan("包路径") 会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类
// includeFilters 指定包含扫描的内容
// excludeFilters 指定不包含的内容
// @Filter 指定过滤规则,type指定扫描的规则(注解,正则,自定义,ASPECTJ表达式),classes指定的扫描的规则类
@ComponentScan(basePackages = {"com.guang"},
includeFilters = @Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = {Repository.class}),
includeFilters = @Filter(type = FilterType.CUSTOM, classes = {FilterCustom.class}),
useDefaultFilters = false)
public class Myconfig {
@Bean("person")
public Person person01() {
return new Person("aiha", 25);
}
}
二、如果我们在使用自定义(includeFilters = @Filter(type = FilterType.CUSTOM, classes = {自己定义的类}))过滤规则的时候,我们自己定义的类要实现TypeFilter接口,例如:
package com.guang.config;
import java.io.IOException;
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;
/**
* 自定义的过滤规则,在自动扫描的时候用,自定义过滤的时候不在使用默认的规则了
*
* @ClassName FilterCustom
* @author xxx
* @date xxx
* @version xxx
*/
public class FilterCustom implements TypeFilter {
/**
*
* @Title: match
* @Description: 覆盖方法注释标签说明
* @param metadataReader 读取的当前正在扫描类的信息
* @param metadataReaderFactory 类工厂中其它类的信息
* @return
* @throws IOException
* @see org.springframework.core.type.filter.TypeFilter#match(org.springframework.core.type.classreading.MetadataReader,
* org.springframework.core.type.classreading.MetadataReaderFactory)
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
// 获取当前类注解的信息
// AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
// 获取当前正在扫描类的信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 获取当前类路径的信息
// Resource resource = metadataReader.getResource();
if (classMetadata.getClassName().startsWith("person")) {
return true;
}
return false;
}
}
-
FilterType.ANNOTATION(默认) | 一组注解,命中所有使用该注解的类,{MyAnno.class} | - |
FilterType.ASSIGNABLE_TYPE | 一组具体类 | - |
FilterType.ASPECTJ | - | 一组表达式,使用Aspectj表达式命中类 |
FilterType.REGEX | - | 一组表达式,使用正则命中类 |
FilterType.CUSTOM | 自定义的TypeFilter. | - |