Spring源码阅读 - @ComponentScan 处理
1. 概述
配置类是 ConfigurationClassPostProcessor
这个 BeanFactoryPostProcessor(BeanDefinitionRefistryPostProcessor)处理的,内部实际是使用了一个 ConfigurationClassParser
进行具体的逻辑实现/处理。
代码从 org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass 开始分析
2. @ComponentScan
略去大部分,仅注释了最常见的。
注意,是可以多个 @ComponentScan 在同一个类上的
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
@AliasFor("basePackages")
String[] value() default {};
// 要扫描的包及其子包, 若要类型安全(避免写错包), 可以使用 basePackageClasses
@AliasFor("value")
String[] basePackages() default {};
// basePackages 的替代. 可考虑在每个 basePackage 中创建一个特殊的标记类或接口, 用于给这个属性引用
Class<?>[] basePackageClasses() default {};
// 被扫描出来的类的 NameGenerator
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
// 略
Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
// 略
ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
// 略
String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;
// 略
boolean useDefaultFilters() default true;
// 略
Filter[] includeFilters() default {};
// 略
Filter[] excludeFilters() default {};
// 略
boolean lazyInit() default false;
// 略
@Retention(RetentionPolicy.RUNTIME)
@Target({})
@interface Filter {
// 略
FilterType type() default FilterType.ANNOTATION;
// 略
@AliasFor("classes")
Class<?>[] value() default {};
// 略
@AliasFor("value")
Class<?>[] classes() default {};
// 略
String[] pattern() default {};
}
}
3. 解析流程
// Process any @ComponentScan annotations
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
// REGISTER_BEAN 阶段,那么和 PARSE_CONFIGURATION 阶段的不同?
if (!componentScans.isEmpty() &&
!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
for (AnnotationAttributes componentScan : componentScans) {
// The config class is annotated with @ComponentScan -> perform the scan immediately
// 指定包扫描,扫描包及其子包,实际最后还是使用 ClassPathBeanDefinitionScanner 来进行扫描的,MyBatis 扫描接口好像也是使用它扫描的
// ClassPathBeanDefinitionScanner 扫描到后会先立即注入BD,也就是这里返回的都是已经注入的BD
// 这里具体扫描逻辑放入了 ComponentScanParser,扫描到的 BeanDefinition 也是内部注入的
// 这里传入了一个 getClassName, 是当仅使用 @ComponentScan 但没有任何配置时默认扫描当前包及其子包
Set<BeanDefinitionHolder> scannedBeanDefinitions =
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
// Check the set of scanned definitions for any further config classes and parse recursively if needed
// 配置类则还需要继续解析, 那么哪些扫描出来的 BD 不会被当做配置类呢?
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
if (bdCand == null) {
bdCand = holder.getBeanDefinition();
}
// 是否是配置类,扫描出来的这些需要判断是否为配置类,进入最初的配置类解析流程
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
parse(bdCand.getBeanClassName(), holder.getBeanName());
}
}
}
}
具体是在 ComponentScanAnnotationParser
中解析的
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, String declaringClass) {
// 诶, 这里就使用到了 AnnotationConfigApplicationContext 中的 scanner, 类路径扫描器, 注意, 每次进来生成一个新的实例进行处理
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
// 接着就是根据 @ComponentScan 的配置对扫描器进行配置, 暂时略过这些配置
Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
BeanUtils.instantiateClass(generatorClass));
ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
scanner.setScopedProxyMode(scopedProxyMode);
}
else {
Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
}
scanner.setResourcePattern(componentScan.getString("resourcePattern"));
for (AnnotationAttributes includeFilterAttributes : componentScan.getAnnotationArray("includeFilters")) {
List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(includeFilterAttributes, this.environment,
this.resourceLoader, this.registry);
for (TypeFilter typeFilter : typeFilters) {
scanner.addIncludeFilter(typeFilter);
}
}
for (AnnotationAttributes excludeFilterAttributes : componentScan.getAnnotationArray("excludeFilters")) {
List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(excludeFilterAttributes, this.environment,
this.resourceLoader, this.registry);
for (TypeFilter typeFilter : typeFilters) {
scanner.addExcludeFilter(typeFilter);
}
}
boolean lazyInit = componentScan.getBoolean("lazyInit");
if (lazyInit) {
scanner.getBeanDefinitionDefaults().setLazyInit(true);
}
// 这里是生成要扫描的包的
// 首先是 basePackages 配置的
Set<String> basePackages = new LinkedHashSet<>();
String[] basePackagesArray = componentScan.getStringArray("basePackages");
for (String pkg : basePackagesArray) {
String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
Collections.addAll(basePackages, tokenized);
}
// 再就是 basePackageClasses 配置的
for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
// 都没有进行配置,则使用配置类所在包
if (basePackages.isEmpty()) {
basePackages.add(ClassUtils.getPackageName(declaringClass));
}
// 排除当前配置类
scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
@Override
protected boolean matchClassName(String className) {
return declaringClass.equals(className);
}
});
// 扫描, 可也以看出, BD 是否注入到容器是在扫描器中进行处理的
return scanner.doScan(StringUtils.toStringArray(basePackages));
}
上面只是对 @ComponentScan 进行了进一步读取处理,下面才是真正的处理逻辑,使用的是 ClassPathBeanDefinitionScanner
,这个类在 AnnotationConfigApplicationContext 中也持有一个实例。
下面也略去大部分注释,仅关注主要逻辑
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
for (String basePackage : basePackages) {
// 这里就扫描出来所有的 BD,并解析、注册,也即过滤过程在内部户已经处理了
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
// 注册进入容器
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
return beanDefinitions;
}
下面是扫描了筛选的流程
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
// 略
if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
}
else {
// 看这里
return scanCandidateComponents(basePackage);
}
}
private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
Set<BeanDefinition> candidates = new LinkedHashSet<>();
try {
// 拼接, 加上了 "classpath*:" 前缀和 "**/*.class" 后缀, 大致可以理解为仅扫描当前类路径下的,且为当前包及其子包下的 class 文件
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + '/' + this.resourcePattern;
// 获取到所有 class 文件
Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
boolean traceEnabled = logger.isTraceEnabled();
boolean debugEnabled = logger.isDebugEnabled();
// 遍历 class
for (Resource resource : resources) {
if (traceEnabled) {
logger.trace("Scanning " + resource);
}
try {
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
// 是否满足条件,这里是 Filter 筛选
if (isCandidateComponent(metadataReader)) {
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setSource(resource);
// 是否满足条件,这里是抽象类等筛选
if (isCandidateComponent(sbd)) {
if (debugEnabled) {
logger.debug("Identified candidate component class: " + resource);
}
candidates.add(sbd);
}
else {
if (debugEnabled) {
logger.debug("Ignored because not a concrete top-level class: " + resource);
}
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not matching any filter: " + resource);
}
}
}
catch (FileNotFoundException ex) {
if (traceEnabled) {
logger.trace("Ignored non-readable " + resource + ": " + ex.getMessage());
}
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to read candidate component class: " + resource, ex);
}
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
}
return candidates;
}
4. 待续
上面仅将大体流程描述了,很多细节没有写,后续再补充细节