大家在spring开发的过程中,肯定使用了大量的注解,比如@ComponentScan、@Import、@Bean、@Configuration、@Conditional等大量注解来帮助我们快速开发,但是大家想过没有,他们底层是如何实现的。
比如之前大家注册一个bean需要在spring.xml配置文件里面配置一堆,现在只需要一个注解就可以完成了。
@Component
public class Cat {
int age;
public Cat() {
System.err.println("Cat初始化成功");
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
上面是如何做到的?它是依靠ConfigurationClassPostProcessor实现的。
ConfigurationClassPostProcessor是一个实现BeanDefinitionRegistryPostProcessor接口的类,在spring容器初始化的时候,调用BeanFactory的后置处理器实现注解扫描的,注册bean定义信息的。
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
PriorityOrdered, ResourceLoaderAware, ApplicationStartupAware, BeanClassLoaderAware, EnvironmentAware {
.............
}
方法如何跟入:
AbstractApplicationContext类:
refresh() ---- invokeBeanFactoryPostProcessors(beanFactory) --- 找到实现BeanDefinitionRegistryPostProcessor和PriorityOrdered接口的方法进行调用invokeBeanDefinitionRegistryPostProcessors方法到ConfigurationClassPostProcessor类进行处理。
核心处理逻辑代码:
protected final SourceClass doProcessConfigurationClass( ConfigurationClass configClass, SourceClass sourceClass, Predicatefilter) throws IOException { if (configClass.getMetadata().isAnnotated(Component.class.getName())) { // Recursively process any member (nested) classes first processMemberClasses(configClass, sourceClass, filter); } // 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.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() + "]. Reason: Environment must implement ConfigurableEnvironment"); } } // Process any @ComponentScan annotations Set 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 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) { BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition(); if (bdCand == null) { bdCand = holder.getBeanDefinition(); } if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) { parse(bdCand.getBeanClassName(), holder.getBeanName()); } } } } // Process any @Import annotations processImports(configClass, sourceClass, getImports(sourceClass), filter, true); // Process any @ImportResource annotations AnnotationAttributes importResource = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class); if (importResource != null) { 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 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 != null && !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; }
下面挑选几个注解说明一下,其他的注解也大同小异。
@ComponentScan
它的出现主要是对标component-scan标签,标签只能使用在xml的配置文件中,而现在越来越流行使用注解的形式注册bean。
- 首先检查传入的bean定义是否实现ComponentScan,如果实现了拿出ComponentScan信息。
- 判断如果不为空 和 是否符合注入条件,这里shouldSkip方法里面应用了@Conditional注解判断条件。
- 进行遍历ComponentScan注解信息,使用componentScanParser解析,找出符合条件的bean定义信息。
- 对找到的bean定义信息进行递归调用,防止找到的bean定义信息上面还有注解信息。
// Process any @ComponentScan annotations
Set 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 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) {
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
if (bdCand == null) {
bdCand = holder.getBeanDefinition();
}
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
parse(bdCand.getBeanClassName(), holder.getBeanName());
}
}
}
}
特别说明一下这个寻找bean定义信息过程方法:
这里扫描使用的ClassPathBeanDefinitionScanner这个扫描器,它的作用是指定过滤条件,在指定的classpath下寻找符合条件的bean定义信息,并且之后注册到registerBeanDefinition集合中。
mybatis框架的@MapperScan扫描mapper也是使用的这个扫描器来实现的。
参考:org.mybatis.spring.mapper.ClassPathMapperScanner



