【问题标题】:Profiles ordering in migration from Spring Boot 2.3 to 2.4从 Spring Boot 2.3 迁移到 2.4 的配置文件排序
【发布时间】:2021-06-12 20:06:17
【问题描述】:

我正在将应用程序从 Spring Boot 2.3 迁移到 Spring Boot 2.4,我在加载配置文件时遇到了一些问题。

在我的应用程序中,我有一些个人资料:

  • default (application-default.yml) :应用程序的默认值。有很多默认值,目的是不让文件 application.yml 复杂化,所以有这个文件很重要
  • 质量(application-quality.yml):质量概况
  • 环境配置文件(例如application-Dev.yml):当前环境的配置文件。它必须覆盖 application.yml 和 application-default.yml 值
  • 环境的数据库配置文件(例如 application-databaseDev.yml):当前环境的数据库连接配置文件

在 Spring Boot 2.3 中,配置为:

application.yml

spring:
  profiles:
    include:
      - default
  redis:
    jedis:
      pool:
        max-active: 4

application-default.yml

spring:
  profiles:
    include:
      - quality
myApplication:
  vcs:
    url: https://default.url 
  cache:
    enabled: true 

application-Dev.yml

spring:
  profiles:
    include:
      - databaseDev
myApplication:
  cache:
    enabled: false 

我正在使用参数--spring.profiles.active=Dev 启动应用程序

在这种情况下,这里是活动配置文件The following profiles are active: default,quality,Dev,databaseDev的顺序

这是我想要的顺序。例如,键 myApplication.cache.enabled 的值为 false

我阅读了迁移到 Spring Boot 2.4 的文档,所以我现在的配置是:

application.yml

spring:
  profiles:
    group:
      "Dev": "default, quality, databaseDev"
  redis:
    jedis:
      pool:
        max-active: 4

application-default.yml

myApplication:
  vcs:
    url: https://default.url 
  cache:
    enabled: true 

application-Dev.yml

myApplication:
  cache:
    enabled: false 

但现在我有活跃的个人资料The following profiles are active: Dev,default,quality,databaseDev

在这种情况下,键 myApplication.cache.enabled 的值为 true

现在配置文件 Dev 不会将 default 配置文件覆盖为 2.3,但相反 default 配置文件会覆盖 Dev 配置文件。

有没有办法将配置修改为 2.3(不使用 use-legacy-processing: true)?我是不是在某个地方弄错了?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    我找到了一个可行的解决方案: 将配置文件 Dev 重命名为 dev 并创建一个名为 Dev 的组

    结果如下:

    application.yml

    spring:
      config:
        import: classpath:application-groups.yml 
      redis:
        jedis:
          pool:
            max-active: 4
    

    application-groups.yml

    spring:
      profiles:
        group:
          "base": "default, quality"
          "Dev": "base, databaseDev"
    

    application-default.yml

    myApplication:
      vcs:
        url: https://default.url 
      cache:
        enabled: true 
    

    application-dev.yml

    myApplication:
      cache:
        enabled: false 
    

    以参数--spring.profiles.active=Dev开头 The following profiles are active: Dev,base,default,quality,databaseDev

    你觉得这个解决方案怎么样?

    【讨论】:

    • 这个答案没有任何意义。 application-dev.yml 永远不会被使用。
    猜你喜欢
    • 2022-01-25
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2015-10-03
    相关资源
    最近更新 更多