如何在 Spring Boot 2.4+ application.properties 中集成 spring 配置文件

How to integrate spring profiles in Spring Boot 2.4+ application.properties

我当前的 spring boot 2.3 及以下配置文件是

我必须在我的 local 个人资料中包含个人资料 onlyLocal,并且我必须在所有其他个人资料中包含 cloudProfile

对于 spring boot 2.4+,我发现将以下内容放入我的 application.properties 文件中是可行的:

spring.profiles.group.local=local,onlyLocal
spring.profiles.group.dev=dev,cloudProfile
spring.profiles.group.qa=qa,cloudProfile
spring.profiles.group.prod=prod,cloudProfile

我的问题是,有better/cleaner方法吗?

使用组将多个配置文件聚合到一个配置文件中非常合理,并且非常符合 Spring 引导团队的意图:https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

一般来说,您应该考虑配置文件组的复杂性是否真的值得。如果配置文件 cloudProfileonlyLocal 只产生少量属性,那么将它们复制到各自的属性文件中可能更有意义。如果这两个配置文件确实包含很多属性,那么复杂性是有保证的,并且可以避免重复。

将属性文件导入其他属性文件的另一种机制是使用 spring.config.import。但它确实用不同的机制做同样的事情。