java - 在 spring boot 2.4.0 版本中包含配置文件

标签 java spring spring-boot profile spring-profiles

作为开发人员,我使用默认的 dev profile 在我的本地开发环境中。这是我的 application-dev.properties 的一部分文件:

# Profiles    
spring.profiles.include=auth
以前我使用 Spring Boot 2.3.0.RELEASE 和 spring.profiles.include包含的属性(property)auth运行时的配置文件。
但是在我迁移到 Spring Boot 2.4.0 之后,我没有得到 auth配置文件已启用。 spring.profiles.include属性(property)似乎不像以前那样工作。
请告诉我如何配置我的配置文件,以便获得与迁移前相同的结果。 (我不想在这里使用配置文件组)
提前致谢!

最佳答案

如果您的配置处理以不兼容的方式更改并且您希望使用“传统”处理方式,您可以通过设置重新启用它:

spring.config.use-legacy-processing=true
或者,使用 YAML:
spring:
  config:
    use-legacy-processing: true
这应该将配置处理恢复到 2.3.x相等的。但是请注意,此属性的存在仅仅是为了简化从 2.3.x 迁移配置文件配置的过程。至 2.4.x并且可能会在 future 的主要版本中被弃用和删除1,因此您仍然应该尝试尽快迁移。要了解此更改的原因和一些其他信息,请继续阅读。
值得注意的是 2.4.0是以下两种范式:

So in Spring Boot 2.4 we’re planning to make two significant changes to the way the properties and YAML files are loaded:

  1. Documents will be loaded in the order that they’re defined.

  2. Profiles can no longer be activated from profile specific documents.


这一变化实际上使什么覆盖什么什么时候逻辑相当容易消化,但导致不得不禁用某些功能。例如:
my.prop: test

---
spring.profiles: prodprops
my.prop: prod

---
spring.profiles: prod
# no longer works - activating a profile from a profile-specific document!
spring.profiles.include: prodprops
  
将导致异常,因为配置尝试从特定于配置文件的文档激活配置文件,这不再被允许。
为了涵盖此用例(和其他用例),已添加配置文件组作为一项功能。这意味着要启用您之前的行为,您需要创建一个配置文件组,如下所示:
spring.profiles.group.<group>=dev, auth
或者,在 YAML 中:
spring:
  profiles:
    group:
      <group>: dev, auth
哪里<group>是您选择的配置文件组的名称。请注意,您可以定义多个组,所有组都应具有不同的名称。如果您随后使用 <group> 启动您的应用程序profile,应该激活属于该组的所有配置文件。
作为旁注,Spring Boot 2.4.0另外添加了对多文档属性文件的支持,如下所示:
test=value
spring.profiles.active=local
#---
spring.config.activate.on-profile=dev
test=overridden value
请注意文档分隔符 ( #--- )。这允许您在 .properties 中拥有类似的覆盖逻辑。文件如 .yml文件。
同样,相关信息 update post 中提供了此信息和其他信息。 .
1 如果先前的弃用是任何指标,则应在 2.5.0 中删除该属性最早或2.6.0最迟,后者更有可能(从 2.5.x 开始弃用)。

关于java - 在 spring boot 2.4.0 版本中包含配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64907675/

相关文章:

java - Spring安全授权

每个请求执行两次 Spring Security Filter 链。为什么?

Spring 启动和 SQLite

java - 如何将数字传递给 TextField JavaFX?

java - Spring AOP - 实现 spring aop 时不调用服务方法

spring-boot - Spring Boots Gradle插件在启动脚本中忽略了applicationDefaultJvmArgs

java - spring boot hibernate启动慢

java - 如何更改框架的大小,来自另一个类

java - MyBatis MapperScannerConfigurer 从类路径扫描基础包

java - 从 WCF 和 Biztalk 中的 WSDL 导入键/值复杂类型定义