This tutorial demonstrates how to active a profile when a related profile is actived in Spring Boot.
Abstract
Spring Boot is being heavily used for development of Microservices since last few years due to its great features. One of such features is profiles management in Spring Boot. For more details on Spring Boot Profiles, please refer Using Profiles in Spring Boot.
However, sometimes we may come across a scenarion where we want to activate a profile based on activation of other profile. For instance, we want to activate configmap profile if kubernetes profile is activated.
Activating Profile based on Other Profile
We will now see how to activate configmap profile if kubernetes profile is activated.
As we know, when a profile is activated, Spring Boot loads application-{profile}.properties file. In our case, if we activate kubernetes profile, Spring Boot will automatically load application-kubernetes.properties file for us.
We can now simply add following property to this file -
spring.profiles.include=configmap
This will make Spring Boot activate configmap file and hence load application-configmap.properties file as well.
We can also use this property to specify multiple profiles (configmap and secrets in our case) as shown below -
spring.profiles.include=configmap,secret
In this way, we can have chain of profiles loaded based on previously loaded profiles.
Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.