/** 通过解析源类中的 注解、成员 和 方法,创建 ConfigurationClass
* Apply processing and build a complete {@link ConfigurationClass} by reading the
* annotations, members and methods from the source class. This method can be called
* multiple times as relevant sources are discovered.
* @param configClass the configuration class being build
* @param sourceClass a source class
* @return the superclass, or {@code null} if none found or previously processed
*/@Nullableprotectedfinal SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)throws IOException {if(configClass.getMetadata().isAnnotated(Component.class.getName())){// 含有 @Component 注解// Recursively process any member (nested) classes first.首先递归处理任何成员(嵌套)类processMemberClasses(configClass, sourceClass);}// Process any @PropertySource annotations. 处理 @PropertySourcefor(AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), PropertySources.class,
org.springframework.context.annotation.PropertySource.class)){if(this.environment instanceofConfigurableEnvironment){processPropertySource(propertySource);}else{
logger.info("Ignoring @PropertySource annotation on ["+ sourceClass.getMetadata().getClassName()+"]. Reason: Environment must implement ConfigurableEnvironment");}}// Process any @ComponentScan annotations. 处理 @ComponentScan 注解
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 neededfor(BeanDefinitionHolder holder : scannedBeanDefinitions){
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();if(bdCand == null){
bdCand = holder.getBeanDefinition();}if(ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand,this.metadataReaderFactory)){// 判断解析获取的 BeanDefinition 中 是否有配置类.也就是说只要有 @Configuration、@Component、@ComponentScan、@Import、@ImportResource 和 @Bean 中的其中一个注解parse(bdCand.getBeanClassName(), holder.getBeanName());// 如果有配置类,递归调用,解析该配置类}}}}// Process any @Import annotations. 处理 @Import 注解processImports(configClass, sourceClass,getImports(sourceClass),true);// Process any @ImportResource annotations. 处理 @ImportResource 注解
AnnotationAttributes importResource =
AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);if(importResource != null){
String[] resources = importResource.getStringArray("locations");
Class<?extendsBeanDefinitionReader> readerClass = importResource.getClass("reader");for(String resource : resources){
String resolvedResource =this.environment.resolveRequiredPlaceholders(resource);
configClass.addImportedResource(resolvedResource, readerClass);}}// Process individual @Bean methods. 处理 @Bean 方法
Set<MethodMetadata> beanMethods =retrieveBeanMethodMetadata(sourceClass);// 检索 @Bean 方法for(MethodMetadata methodMetadata : beanMethods){
configClass.addBeanMethod(newBeanMethod(methodMetadata, configClass));// 创建 BeanMethod,添加到 beanMethods}// Process default methods on interfacesprocessInterfaces(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 recursereturn sourceClass.getSuperClass();// 不返回null,外层循环调用}}// No superclass -> processing is completereturn null;// 循环出口.没有父类,处理完成,返回null,外层结束循环}