SpringBoot 自动配置失效的解决方法

 更新时间:2021年11月17日 15:53:02   作者:glmapper  
本文主要介绍了SpringBoot 自动配置失效的解决方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文源自近期项目中遇到的问题, bug 总是出现在你自以为是的地方...

问题描述

下面是一个简单复现的代码片段,在你没有阅读完本文时,如果能做出正确的判断,那恭喜你可以节省阅读本文的时间了。

1、自动配置类:AutoTestConfiguration

@Configuration
@EnableConfigurationProperties(TestProperties.class)
@ConditionalOnProperty(prefix = "test", name = "enable")
public class AutoTestConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public TestBean testBean(TestProperties properties){
        System.out.println("this is executed.....");
        return new TestBean();
    }
}

2、配置类 TestProperties

@ConfigurationProperties(prefix = "test")
public class TestProperties {
    private boolean enable = true;
    public boolean isEnable() {
        return enable;
    }
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}

这两个类都在 root package 下,可以保证能够正常被 Spring 扫描到;那么问题是 TestBean 会不会被正常创建?当然这里的结论是不会。

可能有的同学会说你的 TestProperties 没有加 @Configuration 注解,Spring 不认识它,那真的是这样吗?很显然也不是。
在排查这个问题的过程中,也有遇到其他问题,也是之前没有遇到过的;即使 Spring 源码我看过很多遍,但是仍然会有一些边边角角让你意想不到的地方;下面就针对这个问题,慢慢来揭开它的面纱。

@EnableConfigurationProperties 注解行为

在之前的版本中,TestProperties 是有被 @Configuration 注解标注的

@Configuration // 可以被 spring 扫描
@ConfigurationProperties(prefix = "test")
public class TestProperties {
    private boolean enable = true;
    public boolean isEnable() {
        return enable;
    }
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}

常规的思路是,当 TestProperties 被扫描到之后,spring env 中就会有 test.enable=true 的 k-v 存在,当执行 AutoTestConfiguration 自动配置类刷新时,@ConditionalOnProperty(prefix = "test", name = "enable") 则会生效,进而 TestBean 被正常创建。

但事实并非如此,下面是对于此问题的验证

配置有效,AutoTestConfiguration 未刷新

两个点:

  • AutoTestConfiguration#testBean 执行会输出一个 log(用于判断 AutoTestConfiguration 是否正常刷新)
  • 监听 ApplicationReadyEvent 事件,拿 test.enable 值(用于判端配置是否正常加载,也就是 TestProperties 是否被正常刷新)

代码如下:

@SpringBootApplication
public class Application implements ApplicationListener<ApplicationReadyEvent> {
   @Autowired
   private ApplicationContext applicationContext;
   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }
   @Override
   public void onApplicationEvent(ApplicationReadyEvent event) {
      System.out.println(this.applicationContext.getEnvironment().getProperty("test.enable")  + "------");
   }
}

执行得到的结果是 AutoTestConfiguration#testBean 没有被执行,但test.enable 为 true。

这里说明 TestProperties 是有被刷新的,但是并没有对 @ConditionalOnProperty 起到作用,那么这里基本可以猜到是自动配置类上的 @ConditionalOnProperty 和 @EnableConfigurationProperties 的作用顺序问题。

在验证顺序问题之前,我尝试在 application.properties 中增加如下配置,re run 项目:

test.enable=true

到这里我得到了另一个 bean 冲突的问题。

prefix-type

异常提示如下:

Parameter 0 of method testBean in com.glmapper.bridge.boot.config.AutoTestConfiguration required a single bean, but 2 were found:
 - testProperties: defined in file [/Users/glmapper/Documents/project/exception-guides/target/classes/com/glmapper/bridge/boot/config/TestProperties.class]
 - test-com.glmapper.bridge.boot.config.TestProperties: defined in null

这里出现了 test-com.glmapper.bridge.boot.config.TestProperties 这个 name 的 bean。我尝试在代码中去检查是否有显示给定这个 bean 名字,但是没有找到,那只有一种可能,就是这个是被 spring 自己创建的。

这个过程在 spring 刷新阶段非常靠前,在排查这个问题时,还是耽误了一些时间,最后还是把问题定位一致前置到 beandefinitions 初始化才找到。

这里是 @EnableConfigurationProperties 注解的一个行为,依赖 EnableConfigurationPropertiesRegistrar,源码如下:

class EnableConfigurationPropertiesRegistrar implements ImportBeanDefinitionRegistrar {
         .getQualifiedAttributeName(EnableConfigurationPropertiesRegistrar.class, "methodValidationExcludeFilter");

   @Override
   public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
      registerInfrastructureBeans(registry);
      registerMethodValidationExcludeFilter(registry);
      ConfigurationPropertiesBeanRegistrar beanRegistrar = new ConfigurationPropertiesBeanRegistrar(registry);
      // to register
      getTypes(metadata).forEach(beanRegistrar::register);
   }

通过代码比较容易看出,EnableConfigurationPropertiesRegistrar 会将目标 metadata 注册成 bean;继续 debug,找到了产生 prefix-type 格式 name 的 bean。

下面是 getName 的具体代码

private String getName(Class<?> type, MergedAnnotation<ConfigurationProperties> annotation) {
    // 拿 prefix
   String prefix = annotation.isPresent() ? annotation.getString("prefix") : "";
   // prefix + "-" + 类全限定名 
   return (StringUtils.hasText(prefix) ? prefix + "-" + type.getName() : type.getName());
}

到这里我们先明确一个问题:

如果你使用 @EnableConfigurationProperties 来开启配置类,那么就不要在配置类上使用@Configuration 等能够被 Spring scan 识别到的注解,以免在后续的使用中同一类型的 bean 多个实例

@ConditionalOnProperty

在回到配置不生效问题上来,这里在官方 issue 是有记录的:github.com/spring-proj…
不过这里还是通过分析代码来还原下问题产生的根本原因;这里主要从两个方面来分析:

  • @ConditionalOnProperty match 值逻辑,需要明确在匹配 value 时,从哪些 PropertySource 读取的。
  • @ConditionalOnProperty match 失败和 bean 刷新的逻辑

@ConditionalOnProperty match 逻辑

首先是 @ConditionalOnProperty 在执行计算时,匹配 value 的值来源问题,通过 debug 代码很容易就得到了所有的 source 来源,如下图:

从 debug 看,本案例有 4 个来源(具体如上图),实际上从源码来看,source 涵盖了 spring env 所有来源:

[ConfigurationPropertySourcesPropertySource {name='configurationProperties'}, 
StubPropertySource {name='servletConfigInitParams'}, 
StubPropertySource {name='servletContextInitParams'}, 
PropertiesPropertySource {name='systemProperties'}, OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}, 
RandomValuePropertySource {name='random'}, 
OriginTrackedMapPropertySource {name='Config resource 'class path resource [application.properties]' via location 'optional:classpath:/''}]

所以本文案例中不生效原因就是上面这些 PropertySource 都没有 test.enable,也就是 TestProperties 没被刷新,或者其在自动配置类之后才刷新。

@ConditionalOnProperty skip 逻辑

这里主要解释 @ConditionalOnPropert 和 bean 被刷新的逻辑关系,具体实现在 ConditionEvaluator 类中

public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationPhase phase) {
    // 1、没有 Conditional 注解,则扫描时不会跳过当前 bean
    // 2、遍历 conditions 进行判断是否满足
}

所以对于自动配置类上的注解,Conditional 是作为当前类是否允许被刷新的前提,只有 Conditional 条件满足,才会将当前的自动配置类加入到待刷新 bean 列表中去,如果 Conditional 不满足,这个 bean 将直接被跳过,不会被放到 BeandefinitonMap 中去,也就不会有后续的刷新动作。

@ConditionalOnProperty 作用时机在 BeanDefiniton 被创建之前,其执行时机要比 @EnableConfigurationProperties 作用要早,这也就说明了,为什么 TestProperties 中 test.enable=true, AutoTestConfiguration 也不会刷新的原因了。

总结

本文通过一个简单 case,对于项目中遇到的 SpringBoot 配置失效导致 bean 未被刷新问题进行了回溯,总结如下:

Conditional 相关注解对于自动配置类来说,作用时机较早,用于决定当前自动配置类是否允许被刷新
@EnableConfigurationProperties enable 的类,会默认注册一个 bean,bean 名字格式为 prefix-type

到此这篇关于SpringBoot 自动配置失效的解决方法的文章就介绍到这了,更多相关SpringBoot 自动配置失效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java实现鼠标随机移动效果的示例代码

    Java实现鼠标随机移动效果的示例代码

    有的时候我们需要鼠标一直滑动的情况,为了节省时间,本文用Java语言写了一个脚本,可以实现鼠标随机移动,感兴趣的小伙伴可以了解一下
    2022-05-05
  • Java之OutputStreamWriter流案例详解

    Java之OutputStreamWriter流案例详解

    这篇文章主要介绍了Java之OutputStreamWriter流案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • 解决MyBatis返回结果类型为Boolean的问题

    解决MyBatis返回结果类型为Boolean的问题

    这篇文章主要介绍了解决MyBatis返回结果类型为Boolean的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • 如何获取jar包resources文件路径

    如何获取jar包resources文件路径

    java项目发布到jar之后找不到文件路径,遇到这样问题如何处理呢,下面小编给大家分享获取jar包resources文件路径的解决方法,感兴趣的朋友一起看看吧
    2023-12-12
  • Java数据类型Integer与int的区别详细解析

    Java数据类型Integer与int的区别详细解析

    这篇文章主要介绍了Java数据类型Integer与int的区别详细解析,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null,int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比,需要的朋友可以参考下
    2023-12-12
  • Hibernate连接三种数据库的配置文件

    Hibernate连接三种数据库的配置文件

    今天小编就为大家分享一篇关于Hibernate连接三种数据库的配置文件,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Spring Security中用户名和密码的验证完整流程

    Spring Security中用户名和密码的验证完整流程

    本文给大家介绍Spring Security中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,感兴趣的朋友一起看看吧
    2025-06-06
  • JavaSE-面向对象(方法重写)

    JavaSE-面向对象(方法重写)

    子类在调用父类的私有方法中不能直接调用,但是可以通过get方法进行调用,修改属性的值可以通过set方法进行修改。而子类想要修改父类中的方法可以使用方法重写进行操作。
    2021-08-08
  • java实现背单词程序

    java实现背单词程序

    这篇文章主要为大家详细介绍了java实现背单词程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Java并发编程示例(十):线程组

    Java并发编程示例(十):线程组

    这篇文章主要介绍了Java并发编程示例(十):线程组,Java提供了ThreadGroup类来控制一个线程组,一个线程组可以通过线程对象来创建,也可以由其他线程组来创建,生成一个树形结构的线程,需要的朋友可以参考下
    2014-12-12

最新评论