ClassPathScanningCandidateComponentProvider 扫描给定包及其子包的类

ClassPathScanningCandidateComponentProvider 类的findCandidateComponents()方法具体实现扫描给定类路径包的功能,主要源码如下:

//保存过滤规则要包含的注解,即Spring默认的@Component、@Repository、@Service、
//@Controller注解的Bean,以及JavaEE6的@ManagedBean和JSR-330的@Named注解
private final List<TypeFilter> includeFilters = new LinkedList<>();

//保存过滤规则要排除的注解
private final List<TypeFilter> excludeFilters = new LinkedList<>();
//构造方法,该方法在子类ClassPathBeanDefinitionScanner的构造方法中被调用
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters) {
	this(useDefaultFilters, new StandardEnvironment());
}

public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) {
	//如果使用Spring默认的过滤规则,则向容器注册过滤规则
	if (useDefaultFilters) {
		registerDefaultFilters();
	}
	setEnvironment(environment);
	setResourceLoader(null);
}
//向容器注册过滤规则
@SuppressWarnings("unchecked")
protected void registerDefaultFilters() {
	//向要包含的过滤规则中添加@Component注解类,注意Spring中@Repository
	//@Service和@Controller都是Component,因为这些注解都添加了@Component注解
	this.includeFilters.add(new AnnotationTypeFilter(Component.class));
	//获取当前类的类加载器
	ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
	try {
		//向要包含的过滤规则添加JavaEE6的@ManagedBean注解
		this.includeFilters.add(new AnnotationTypeFilter(
				((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
		logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
	}
	catch (ClassNotFoundException ex) {
		// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
	}
	try {
		//向要包含的过滤规则添加@Named注解
		this.includeFilters.add(new AnnotationTypeFilter(
				((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
		logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
	}
	catch (ClassNotFoundException ex) {
		// JSR-330 API not available - simply skip.
	}
}
//扫描给定类路径的包
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
	if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
		return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
	}
	else {
		return scanCandidateComponents(basePackage);
	}
}
private Set<BeanDefinition> addCandidateComponentsFromIndex(CandidateComponentsIndex index, String basePackage) {
	//创建存储扫描到的类的集合
	Set<BeanDefinition> candidates = new LinkedHashSet<>();
	try {
		Set<String> types = new HashSet<>();
		for (TypeFilter filter : this.includeFilters) {
			String stereotype = extractStereotype(filter);
			if (stereotype == null) {
				throw new IllegalArgumentException("Failed to extract stereotype from "+ filter);
			}
			types.addAll(index.getCandidateTypes(basePackage, stereotype));
		}
		boolean traceEnabled = logger.isTraceEnabled();
		boolean debugEnabled = logger.isDebugEnabled();
		for (String type : types) {
			//为指定资源获取元数据读取器,元信息读取器通过汇编(ASM)读//取资源元信息
			MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(type);
			//如果扫描到的类符合容器配置的过滤规则
			if (isCandidateComponent(metadataReader)) {
				//通过汇编(ASM)读取资源字节码中的Bean定义元信息
				AnnotatedGenericBeanDefinition sbd = new AnnotatedGenericBeanDefinition(
						metadataReader.getAnnotationMetadata());
				if (isCandidateComponent(sbd)) {
					if (debugEnabled) {
						logger.debug("Using candidate component class from index: " + type);
					}
					candidates.add(sbd);
				}
				else {
					if (debugEnabled) {
						logger.debug("Ignored because not a concrete top-level class: " + type);
					}
				}
			}
			else {
				if (traceEnabled) {
					logger.trace("Ignored because matching an exclude filter: " + type);
				}
			}
		}
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
	}
	return candidates;
}
//判断元信息读取器读取的类是否符合容器定义的注解过滤规则
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
	//如果读取的类的注解在排除注解过滤规则中,返回false
	for (TypeFilter tf : this.excludeFilters) {
		if (tf.match(metadataReader, getMetadataReaderFactory())) {
			return false;
		}
	}
	//如果读取的类的注解在包含的注解的过滤规则中,则返回ture
	for (TypeFilter tf : this.includeFilters) {
		if (tf.match(metadataReader, getMetadataReaderFactory())) {
			return isConditionMatch(metadataReader);
		}
	}
	//如果读取的类的注解既不在排除规则,也不在包含规则中,则返回false
	return false;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值