spring包扫描规则

包扫描详解

<contextcomponent-scan base-package =“xx.test.*”use-default-filters =“false”>

base-package:要扫描的包
use-default-filters:是否使用默认的过滤器,默认为true,即扫描@Component, @Repository, @Service, @Controller这些注解的Bean
context:include-filter:使用白名单过滤器
context:exclude-filter:使用黑名单过滤器

Spring 的使用顺序是: 先 exclude-filter,再到include-filter。
在源码的位置内容里面有org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider的方法里:

protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
    for (TypeFilter tf : this.excludeFilters) {
        if (tf.match(metadataReader, this.metadataReaderFactory)) {
            return false;
        }
    }
    for (TypeFilter tf : this.includeFilters) {
        if (tf.match(metadataReader, this.metadataReaderFactory)) {
            AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
            if (!metadata.isAnnotated(Profile.class.getName())) {
                return true;
            }
            AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
            return this.environment.acceptsProfiles(profile.getStringArray("value"));
        }
    }
    return false;
}

具体其他类型还有:

type=`annotation`
type=`assignable`
type=`aspectj`
type=`regex`
type=`custom`

猜你喜欢

转载自blog.csdn.net/baidu_23086307/article/details/80332692
今日推荐