前言
搞明白bean的生命周期后,就去看了下spring怎么发现注释并解析,从而将对应组件注册进容器中的。比如定义一个配置类如下,spring是如何检索到每个@Bean注解并将其对应bd生成的,以及ComponentScan怎么实现Componen检索等
@Configuration
@ComponentScan("hx.spring")
@EnableAspectJAutoProxy
public class AspectConfig {
@Bean
public A a(){
return new A();
}
@Bean
public B b(){
return new B();
}
@Bean
public LogAspect logAspect(){
return new LogAspect();
}
@Bean
public MyApplicationListener myApplicationListener(){
return new MyApplicationListener();
}
}
注释解析流程
由于对Bean生命周期源码已经比较熟了,通过debug跟代码的方式,能够很快的解决上面的疑惑,我对整个流程梳理成流程图如下
对流程图进行一些说明
- 黄色为解析注释流程,主要是配置类的解析
- 可以看到,解析是在
invokeBeanFactoryPostProcessors
这一步完成的,即调用工厂增强器 - 通过类型从容器中获取到配置类增强器的bd,getBean后直接拿来使用
- 调用增强器的增强方法,主要方法为
doProcessConfigutationClass
完成注释解析的 - 并且,我们还可以探索出该配置增强器的bd,是在Context初始化时,通过新建freader对象,默认放入容器中的,即图中绿色部分
那么具体的解析实现可以看doProcessConfigutationClass
的源码,如下
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
throws IOException {
// Recursively process any member (nested) classes first
processMemberClasses(configClass, sourceClass);
// Process any @PropertySource annotations
for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), PropertySources.class,
org.springframework.context.annotation.PropertySource.class)) {
if (this.environment instanceof ConfigurableEnvironment) {
processPropertySource(propertySource);
}
else {
logger.warn("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
"]. Reason: Environment must implement ConfigurableEnvironment");
}
}
// Process any @ComponentScan annotations
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
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
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
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
if (ConfigurationClassUtils.checkConfigurationClassCandidate(
holder.getBeanDefinition(), this.metadataReaderFactory)) {
parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
}
}
}
}
// Process any @Import annotations
processImports(configClass, sourceClass, getImports(sourceClass), true);
// Process any @ImportResource annotations
if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) {
AnnotationAttributes importResource =
AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
String[] resources = importResource.getStringArray("locations");
Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
for (String resource : resources) {
String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
configClass.addImportedResource(resolvedResource, readerClass);
}
}
// Process individual @Bean methods
Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
for (MethodMetadata methodMetadata : beanMethods) {
configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
}
// Process default methods on interfaces
processInterfaces(configClass, sourceClass);
// Process superclass, if any
if (sourceClass.getMetadata().hasSuperClass()) {
String superclass = sourceClass.getMetadata().getSuperClassName();
if (!superclass.startsWith("java") && !this.knownSuperclasses.containsKey(superclass)) {
this.knownSuperclasses.put(superclass, configClass);
// Superclass found, return its annotation metadata and recurse
return sourceClass.getSuperClass();
}
}
// No superclass -> processing is complete
return null;
}
大概知道在哪里执行的解析就行了,先应付面试,以后用到再深入吧
这部分在springboot自动装配解析中也会用到