Search code examples
javaspring-bootspring-boot-2

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


My current spring boot 2.3 and below config files are

  • application.properties
  • application-dev.properties
  • application-qa.properties
  • application-prod.properties
  • application-local.properties

I have to include the profile onlyLocal in my local profile and I have to include cloudProfile in all other profiles.

For spring boot 2.4+, I found that putting the below in my application.properties file works:

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

My question is, is there a better/cleaner way?


Solution

  • Using groups to aggregate multiple profile files into one profile is perfectly sound and agrees very nicely with the intentions of the Spring Boot team: https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

    Generally, you should think about whether the complexity of profile groups is really worth it. If the profiles cloudProfile and onlyLocal only yield a handful of properties, then it might make more sense to just copy them into the respective properties files. If the two profiles do contain a lot of properties, then the complexity is warranted and allows you to avoid duplication.

    Another mechanism to import properties files into other properties files is using spring.config.import. But it's really doing the same with a different mechanism.