• 根据条件加载 bean:即 @ConditionalOnXxx 的解析


    (version: SpringBoot 2.2.2.RELEASE)

    加载 @Configration 类,或者 @Configuration 类里面的 beanMethod 时,通常会用到条件加载,即:@CondtionalOnXxx

    1. org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForConfigurationClass()
        1.1 org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.TrackedConditionEvaluator#shouldSkip()

    具体的实现代码如下:
    org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.TrackedConditionEvaluator#shouldSkip()

    public boolean shouldSkip(ConfigurationClass configClass) {
        Boolean skip = this.skipped.get(configClass);
        if (skip == null) {
            if (configClass.isImported()) {
                boolean allSkipped = true;
                for (ConfigurationClass importedBy : configClass.getImportedBy()) {
                    if (!shouldSkip(importedBy)) {
                        allSkipped = false;
                        break;
                    }
                }
                if (allSkipped) {
                    // The config classes that imported this one were all skipped, therefore we are skipped...
                    skip = true;
                }
            }
            if (skip == null) {
                // 通过表达式来判断是否要 skip:即 @ConditionalOnBean、@ConditionOnClass、@ConditionalOnMissingClass、@ConditionalOnExpression 等的处理
                skip = conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN);
            }
            this.skipped.put(configClass, skip);
        }
        return skip;
    }
  • 相关阅读:
    Spring spEL
    Spring 使用外部部署文件
    Spring 自动装配
    spring 属性配置细节
    hdu 1054 Strategic Game
    fzu 2037 Maximum Value Problem
    将博客搬至CSDN
    HDU 4714 Tree2Cycle
    HDU 1009 The Shortest Path in Nya Graph
    POJ 1942 Paths on a Grid 组合数的优化
  • 原文地址:https://www.cnblogs.com/kevin-yuan/p/12431852.html
Copyright © 2020-2023  润新知