简单的看一下@ComponentScan注解的源码如下
/**
* Indicates whether automatic detection of classes annotated with {@code @Component}
* {@code @Repository}, {@code @Service}, or {@code @Controller} should be enabled.
*/
//useDefaultFilters默认是true,扫描带有@Component ro @Repository @Service @Controller 的组件
boolean useDefaultFilters() default true;
/**
* Specifies which types are eligible for component scanning.
* <p>Further narrows the set of candidate components from everything in
* {@link #basePackages()} to everything in the base packages that matches
* the given filter or filters.
* @see #resourcePattern()
*/
//指定扫描的时候只需要包含哪些组件
Filter[] includeFilters() default {};
/**
* Specifies which types are not eligible for component scanning.
* @see #resourcePattern()
*/
//指定扫描的时候不需要包含哪些组件
Filter[] excludeFilters() default {};
/**
* Specify whether scanned beans should be registered for lazy initialization.
* <p>Default is {@code false}; switch this to {@code true} when desired.
* @since 4.1
*/
//默认关闭懒加载模式
boolean lazyInit() default false;
/**
* Declares the type filter to be used as an {@linkplain ComponentScan#includeFilters()
* include filter} or {@linkplain ComponentScan#excludeFilters() exclude filter}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({})
@interface Filter {
/**
* The type of filter to use. Default is {@link FilterType#ANNOTATION}.
*/
//扫描默认按照注解扫描
FilterType type() default FilterType.ANNOTATION;
自定义TypeFilter指定过滤规则
下面的源码是FilterType所定义的扫描规则
/**
* Filter candidates marked with a given annotation.
* @see org.springframework.core.type.filter.AnnotationTypeFilter
*/
ANNOTATION,//按照注解扫描
/**
* Filter candidates assignable to a given type.
* @see org.springframework.core.type.filter.AssignableTypeFilter
*/
ASSIGNABLE_TYPE,//按照类型扫描
/**
* Filter candidates matching a given AspectJ type pattern expression.
* @see org.springframework.core.type.filter.AspectJTypeFilter
*/
ASPECTJ,//按照ASPECTJ表达式
/**
* Filter candidates matching a given regex pattern.
* @see org.springframework.core.type.filter.RegexPatternTypeFilter
*/
REGEX,//按照正则表达式
/** Filter candidates using a given custom
* {@link org.springframework.core.type.filter.TypeFilter} implementation.
*/
CUSTOM//按照自定义规则
下面举几个例子
扫描的时候需要包含指定的组件
配置类如下(使用includeFilters)
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import chd.Service.BookService;
import org.springframework.context.annotation.ComponentScan.Filter;
import spring.Person;
@Configuration
@ComponentScan(value = "chd",
//excludeFilters = {@Filter(type=FilterType.ANNOTATION,classes = {Controller.class})})
includeFilters = {@Filter(type=FilterType.ASSIGNABLE_TYPE,classes = {BookService.class})}
,useDefaultFilters = false)
//includeFilters 指定扫描的时候不需要包含哪些组件 用的时候需要把默认组件属性关闭useDefaultFilters = false
//excludeFilters 指定扫描的时候只需要包含哪些组件
//FilterType.ANNOTATION 按照注解
//FilterType.ASSIGNABLE_TYPE 按照类型
public class MainConfig {
// @Bean(value="person")
// public Person person01() {
// return new Person("xbh","25");
// }
}
excludeFilters 指定扫描的时候不需要包含哪些组件
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import chd.Service.BookService;
import org.springframework.context.annotation.ComponentScan.Filter;
import spring.Person;
@Configuration
@ComponentScan(value = "chd",
excludeFilters = {@Filter(type=FilterType.ANNOTATION,classes = {Controller.class})})
//FilterType.ANNOTATION 按照注解
//FilterType.ASSIGNABLE_TYPE 按照类型
public class MainConfig {
}