spring的compentScan注解扫描类机制
1主要通过ConfigurationClassParser类的doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)⽅法来扫描被ComponentScan注解的类。
2 此⽅法在spring容器的切⼊点为ConfigurationClassPostProcessor类,此类实现了接⼝BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor,所以在spring启动时,会调⽤该类来插⼊配置扫描制定包下⾯的类,⽣成BeanDefinition。
3 真正处理扫描类为t.annotation.ClassPathBeanDefinitionScanner,该类的核⼼⽅法是:protected
Set<BeanDefinitionHolder> basePackages),
该⽅法把扫描制定包到的BeanDefinition,然后注⼊到 BeanDefinitionRegistry中
/**
* Perform a scan within the specified base packages,
* returning the registered bean definitions.
* <p>This method does <i>not</i> register an annotation config processor
* but rather leaves this up to the caller.
* @param basePackages the packages to check for annotated classes
* @return set of beans registered if any for tooling registration purposes (never {@code null})
*/
protected Set<BeanDefinitionHolder> basePackages) {
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
for (String basePackage : basePackages) {
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
ScopeMetadata scopeMetadata = solveScopeMetadata(candidate);
springframework包candidate.ScopeName());
String beanName = ateBeanName(candidate, istry);
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, istry);
beanDefinitions.add(definitionHolder);
registerBeanDefinition(definitionHolder, istry);
}
}
}
return beanDefinitions;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。