Spring组件注册之注解@ComponentScan的源码及扫描策略

简单的看一下@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 {
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微微笑再加油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值