spring.profiles.active与spring.config.activate.on-profile之间有什么区别?

13

spring.profiles.active

spring.config.activate.on-profile

的确切区别是什么?

"警告","消息":"属性 'spring.profiles' 从位置 '类路径资源[application.yaml]' 导入无效,应该用 'spring.config.activate.on-profile' 替换 [来源: 类路径资源[application.yaml]


2
我认为它们都有相同的作用,但是spring.profiles适用于Spring Boot 2.3及以下版本,而spring.config.activate.on-profile适用于Spring Boot 2.4及以上版本。更多信息请参见:https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4 - Kris
如果我们在应用的yml文件中同时拥有spring.profiles.active=local和spring.config.activate.on-profile=local,哪一个会优先生效? - newcoder
1个回答

24

spring.profiles.active 可以用于指定哪些配置文件始终处于活动状态。
以下是文档中的示例:

spring:
  profiles:
    active: "production"

spring.config.activate.on-profile (在Spring Boot 2.4之前被称为spring.profiles)可以用于标记配置文件片段是特定于某个profile的。
来自文档的一个示例:

server:
  port: 9000
---
spring:
  config:
    activate:
      on-profile: "development"
server:
  port: 9001
---
spring:
  config:
    activate:
      on-profile: "production"
server:
  port: 0

In the preceding example, the default port is 9000. However, if the Spring profile called ‘development’ is active, then the port is 9001. If ‘production’ is active, then the port is 0.

Spring Boot 2.4 中的另一个变化是不再允许在 spring.config.activate.on-profile 的组合中使用 spring.profiles.active 属性。(有关详细信息,请参见此博客文章。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接