将 springboot 升级到 2.4.2 版后,@ActiveProfiles 注释不起作用

时间:2021-02-10 15:58:12

标签: java spring-boot spring-test

以前我们的应用程序基于 springboot 2.2.2 版,我们的测试之一是使用 aws 覆盖 @ActiveProfiles("test") 文件中的 SPRING_PROFILES_ACTIVE 属性配置。一切正常。

application.xml

但是我们把springboot升级到2.4.2版本后就不行了,在我们的测试中只能把spring.profiles.active读成空字符串,而以前是可以读作字符串“test”。

springboot 2.4.2版本的@ActiveProfiles注解应该怎么做?非常感谢。

1 个答案:

答案 0 :(得分:4)

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

  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 个配置文件处于活动状态:abcd

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