Spring5源码解析6ConfigurationClassParser-解析配置类

37次阅读

共计 13793 个字符,预计需要花费 35 分钟才能阅读完成。

ConfigurationClassParser

ConfigurationClassPostProcessor#processConfigBeanDefinitions 方法中创建了 ConfigurationClassParser 对象并调用其 parse 方法。该方法就是在负责解析配置类、扫描包、注册BeanDefinition,源码如下:

//ConfigurationClassParser#parseSet<BeanDefinitionHolder>) 方法源码
public void parse(Set<BeanDefinitionHolder> configCandidates) {for (BeanDefinitionHolder holder : configCandidates) {BeanDefinition bd = holder.getBeanDefinition();
        try {
            // 根据不同的 BeanDefinition 实例对象 调用不同的 parse 方法
            // 底层其实都是在调用 org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass
            if (bd instanceof AnnotatedBeanDefinition) {parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
            } else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
            } else {parse(bd.getBeanClassName(), holder.getBeanName());
            }
        } catch (BeanDefinitionStoreException ex) {throw ex;} catch (Throwable ex) {
            throw new BeanDefinitionStoreException("Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
        }
    }

    // 执行 DeferredImportSelector
    this.deferredImportSelectorHandler.process();}

在该方法内部根据不同的 BeanDefinition 实例对象,调用了不同的 parse 方法,而这些 parse 方法底层,实际上都是调用了 ConfigurationClassParser#processConfigurationClass 方法。

protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
    // 是否需要跳过 @Conditional
    if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {return;}

    // 第一次进入的时候, configurationClasses size = 0,existingClass 肯定为 null
    ConfigurationClass existingClass = this.configurationClasses.get(configClass);
    if (existingClass != null) {if (configClass.isImported()) {if (existingClass.isImported()) {existingClass.mergeImportedBy(configClass);
            }
            // Otherwise ignore new imported config class; existing non-imported class overrides it.
            return;
        } else {
            // Explicit bean definition found, probably replacing an import.
            // Let's remove the old one and go with the new one.
            this.configurationClasses.remove(configClass);
            this.knownSuperclasses.values().removeIf(configClass::equals);
        }
    }

    // Recursively process the configuration class and its superclass hierarchy.
    SourceClass sourceClass = asSourceClass(configClass);
    do {
        // 真正的做解析
        sourceClass = doProcessConfigurationClass(configClass, sourceClass);
    }
    while (sourceClass != null);

    this.configurationClasses.put(configClass, configClass);
}

方法传入的 ConfigurationClass 对象是对配置类的封装。首先判断配置类上是否有 @Conditional 注解,是否需要跳过解析该配置类。

然后,调用 doProcessConfigurationClass(configClass, sourceClass); 做真正的解析。其中,configClass是程序的配置类,而 sourceClass 是通过 configClass 创建的。

protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
        throws IOException {

    // @Configuration 继承了 @Component
    if (configClass.getMetadata().isAnnotated(Component.class.getName())) {// Recursively process any member (nested) classes first
        // 递归处理内部类
        processMemberClasses(configClass, sourceClass);
    }

    // Process any @PropertySource annotations
    // 处理 @PropertySource
    // @PropertySource 注解用来加载 properties 文件
    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<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) {BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
                if (bdCand == null) {bdCand = holder.getBeanDefinition();
                }
                // 判断解析获取的 BeanDefinition 中 是否有配置类
                // 这里的配置类包括 FullConfigurationClass 和 LiteConfigurationClass
                // 也就是说只要有 @Configuration、@Component、@ComponentScan、@Import、@ImportResource 和 @Bean 中的其中一个注解
                if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
                    // 如果有配置类, 递归调用, 解析该配置类, 这个 if 几乎都为 true,这个方法几乎都要执行
                    parse(bdCand.getBeanClassName(), holder.getBeanName());
                }
            }
        }
    }

    // Process any @Import annotations
    processImports(configClass, sourceClass, getImports(sourceClass), 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
    // 处理单个 @Bean 的方法
    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 != 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;
}

解析内部类

配置类上有 @Configuration 注解,该注解继承 @Component,if 判断为 true,调用processMemberClasses 方法,递归解析配置类中的内部类。

解析 @PropertySource 注解

如果配置类上有 @PropertySource 注解,则解析加载 properties 文件,并将属性添加到 Spring 上下文中。((ConfigurableEnvironment) this.environment).getPropertySources().addFirstPropertySource(newSource);

处理 @ComponentScan 注解

获取配置类上的 @ComponentScan 注解,判断是否需要跳过。循环所有的 ComponentScan,立即执行扫描。ComponentScanAnnotationParser#parse方法如下:

public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
    // 创建 ClassPathBeanDefinitionScanner
    // 在 AnnotationConfigApplicationContext 的构造器中也创建了一个 ClassPathBeanDefinitionScanner
    // 这里证明了, 执行扫描 scanner 不是构造器中的, 而是这里创建的
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
            componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);

    // @ComponentScan 中可以注册自定义的 BeanNameGenerator
    // 但是需要注意, 通过源码可以明白, 这里注册的自定义 BeanNameGenerator 只对当前 scanner 有效
    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 filter : componentScan.getAnnotationArray("includeFilters")) {for (TypeFilter typeFilter : typeFiltersFor(filter)) {scanner.addIncludeFilter(typeFilter);
        }
    }
    for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) {for (TypeFilter typeFilter : typeFiltersFor(filter)) {scanner.addExcludeFilter(typeFilter);
        }
    }

    boolean lazyInit = componentScan.getBoolean("lazyInit");
    if (lazyInit) {scanner.getBeanDefinitionDefaults().setLazyInit(true);
    }

    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);
    }

    // @ComponentScan(basePackageClasses = Xx.class)
    // 可以指定 basePackageClasses, 只要是与是这几个类所在包及其子包, 就可以被 Spring 扫描
    // 经常会用一个空的类来作为 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);
        }
    });

    // 执行扫描
    return scanner.doScan(StringUtils.toStringArray(basePackages));
}

挑一些我觉得是重点的地方记录一下:

  1. parse方法中新创建了一个 ClassPathBeanDefinitionScanner 对象,而在 AnnotationConfigApplicationContext 的构造器中也创建了一个 ClassPathBeanDefinitionScanner 对象,这里证实了在 Spring 内部,真正执行扫描的不是 AnnotationConfigApplicationContext 中的 scanner。
  2. 通过源码可以了解到,在 @ComponentScan 中是可以注册自定义的 BeanNameGenerator的,而这个 BeanNameGenerator 只对当前 scanner 有效。也就是说,这个 BeanNameGenerator 只能影响通过该 scanner 扫描的路径下的 bean 的 BeanName 生成规则。
  3. 最后调用 scanner.doScan(StringUtils.toStringArray(basePackages)); 方法执行真正的扫描,方法返回扫描获取到的BeanDefinition

检验获得的 BeanDefinition 中是否有配置类

检验扫描获得的 BeanDefinition 中是否有配置类,如果有配置类,这里的配置类包括 FullConfigurationClass 和 LiteConfigurationClass。(也就是说只要有 @Configuration@Component@ComponentScan@Import@ImportResource@Bean中的其中一个注解),则递归调用 parse 方法,进行解析。

解析 @Import 注解

processImports(configClass, sourceClass, getImports(sourceClass), true);

processImports方法负责对 @Import 注解进行解析。configClass是配置类,sourceClass又是通过 configClass 创建的,getImports(sourceClass)sourceClass 获取所有的 @Import 注解信息,然后调用ConfigurationClassParser#processImports

// ConfigurationClassParser#processImports 源码
private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
                            Collection<SourceClass> importCandidates, boolean checkForCircularImports) {if (importCandidates.isEmpty()) {return;}

    if (checkForCircularImports && isChainedImportOnStack(configClass)) {this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
    } else {this.importStack.push(configClass);
        try {
            // importCandidates 是 @Import 的封装
            // 循环 importCandidates 对 import 的内容进行分类
            for (SourceClass candidate : importCandidates) {
                // import 导入实现 ImportSelector 接口的类
                if (candidate.isAssignable(ImportSelector.class)) {
                    // Candidate class is an ImportSelector -> delegate to it to determine imports
                    Class<?> candidateClass = candidate.loadClass();
                    // 反射创建这个类的实例对象
                    ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
                    // 是否有实现相关 Aware 接口, 如果有, 这调用相关方法
                    ParserStrategyUtils.invokeAwareMethods(selector, this.environment, this.resourceLoader, this.registry);
                    // 延迟加载的 ImportSelector
                    if (selector instanceof DeferredImportSelector) {
                        //  延迟加载的 ImportSelector 先放到 List 中, 延迟加载
                        this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
                    } else {
                        // 普通的 ImportSelector , 执行其 selectImports 方法, 获取需要导入的类的全限定类名数组
                        String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
                        Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames);
                        // 递归调用
                        processImports(configClass, currentSourceClass, importSourceClasses, false);
                    }
                    // 是否为 ImportBeanDefinitionRegistrar
                } else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
                    // Candidate class is an ImportBeanDefinitionRegistrar ->
                    // delegate to it to register additional bean definitions
                    Class<?> candidateClass = candidate.loadClass();
                    ImportBeanDefinitionRegistrar registrar =
                            BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class);
                    ParserStrategyUtils.invokeAwareMethods(registrar, this.environment, this.resourceLoader, this.registry);
                    // 添加到成员变量 org.springframework.context.annotation.ConfigurationClass.importBeanDefinitionRegistrars 中
                    configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
                } else {
                    // Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
                    // process it as an @Configuration class
                    // 普通 @Configuration class
                    this.importStack.registerImport(currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
                    // 解析导入的 @Configuration class
                    processConfigurationClass(candidate.asConfigClass(configClass));
                }
            }
        } catch (BeanDefinitionStoreException ex) {throw ex;} catch (Throwable ex) {
            throw new BeanDefinitionStoreException(
                    "Failed to process import candidates for configuration class [" +
                            configClass.getMetadata().getClassName() + "]", ex);
        } finally {this.importStack.pop();
        }
    }
}

解析 @ImportResource 注解

@ImportResource注解可以导入 xml 配置文件。

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);
    }
}

解析 @Bean 方法

Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
for (MethodMetadata methodMetadata : beanMethods) {configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
}

@Bean 方法转化为 BeanMethod 对象,添加到 ConfigurationClass#beanMethods 集合中。

如果有父类,则解析父类

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();}
}

如果有父类则返回父类 Class 对象,继续调用该方法。直到返回 null,外层循环结束。

do {
    // 真正的做解析
    sourceClass = doProcessConfigurationClass(configClass, sourceClass);
}
while (sourceClass != null);

源码学习笔记:https://github.com/shenjianen…

欢迎各位关注公众号。

正文完
 0