Home » Java » Enterprise Java » Advanced profile management in Spring Boot

About Mohamed Sanaulla

Mohamed Sanaulla

Advanced profile management in Spring Boot

We all are aware of profile management in Spring Boot and the flexibility it provides in configuring our applications for different environments. The other powerful aspect of this is that at any given time we can have multiple active profiles. The advantage this gives is that we can mix the deployment environment profile along with business use case related profiles.

Let us assume we will have different deployments of the application in the same environment and some properties are going to change based on the deployment no matter if they are in the same environment. In such scenarios, we can have environment-specific application properties files and then each such file can override the properties which change based on different deployment.

I have defined three application properties files as shown below:

#application.properties
app.name=Default
spring.profiles.active=test,org1
#application-local.properties
app.name=Local
#application-test.yml
app:
  name: Test
---
spring:
  profiles: org1
app:
  name: Test Org1
---
spring:
  profiles: org2
app:
  name: Test Org2

Then we have a simple class AdvancedPropsDemo with the main method which prints the value of the property app.name:

@SpringBootApplication
@Component
public class AdvancedPropsDemo implements ApplicationRunner {

    @Value("${app.name}")
    String appName;

    public static void main(String[] args) {
        new SpringApplication(AdvancedPropsDemo.class).run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("App Name value " + appName);
    }
}

We have settest,org1 as active profile and Spring Boot has intelligently picked application-test.yml file and then picked the app.name property defined in the org1 profile. In YAML property files we can create different sections for different profiles in the same file and override the required properties in their corresponding profile section

The complete code can be found here.

Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: Advanced profile management in Spring Boot

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of