【问题标题】:@ActiveProfiles annotation doesn't work after I upgrade my springboot to version 2.4.2将 springboot 升级到 2.4.2 版本后,@ActiveProfiles 注释不起作用
【发布时间】:2021-02-10 15:58:12
【问题描述】:

之前我们的应用程序是基于 springboot 2.2.2 版本的,我们的一项测试是使用 @ActiveProfiles("test") 覆盖我们的 application.xml 文件中的 SPRING_PROFILES_ACTIVE 属性配置。一切都很完美。

spring.profiles.active=${SPRING_PROFILES_ACTIVE:}

但是在我们将 springboot 升级到 2.4.2 版本后,它就不再工作了,我们在测试中只能将 spring.profiles.active 读取为空字符串,但之前它可以读作字符串“test”。

在springboot 2.4.2版本中,要如何让@ActiveProfiles注解生效?非常感谢。

【问题讨论】:

    标签: java spring-boot spring-test


    【解决方案1】:

    您受到Spring Boot issue 19537 修复的影响。为了修复名称中带有逗号的支持配置文件,活动配置文件现在设置为单独的索引属性。例如,如果您激活配置文件a、b,c 和d,将设置三个属性:

    1. spring.profiles.active[0]=a
    2. spring.profiles.active[1]=b,c
    3. spring.profiles.active[2]=d

    在此修复之前,spring.profiles.active 将设置为 a,b,c,d,这将导致 4 个配置文件处于活动状态:a、b、c 和 d。

    我建议您查看要查看 spring.profiles.active 属性的代码。直接访问属性的值是相当不寻常的。更典型的做法是在Environment 上调用getActiveProfiles()。

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 2014-11-28
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多