Saturday, 4 August 2018

Spring boot: Activate profiles programmatically

There are couple of ways to set the active profiles in spring boot application.
Before running your application,

a.   You can call 'SpringApplication.setAdditionalProfiles(…)' method,
b.   Activate profiles by using Spring’s ConfigurableEnvironment interface

Using 'SpringApplication.setAdditionalProfiles(…)' method
Below statements set dev, prod profiles to the application.

SpringApplication application = new SpringApplication(Application.class);
application.setAdditionalProfiles("dev","prod");
ConfigurableApplicationContext applicationContext = application.run(args);

Find the below working example.

DevProfile.java
package com.sample.myApp.model;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("dev")
public class DevProfile {

 public DevProfile() {
  System.out.println("********************************");
  System.out.println("Dev profile is called");
  System.out.println("********************************");
 }
}

ProdProfile.java
package com.sample.myApp.model;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("prod")
public class ProdProfile {
 public ProdProfile() {
  System.out.println("********************************");
  System.out.println("Prod profile is called");
  System.out.println("********************************");
 }
}

TestProfile.java
package com.sample.myApp.model;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("test")
public class TestProfile {
 public TestProfile() {
  System.out.println("********************************");
  System.out.println("Test profile is called");
  System.out.println("********************************");
 }
}

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  SpringApplication application = new SpringApplication(Application.class);
  application.setAdditionalProfiles("dev","prod");
  ConfigurableApplicationContext applicationContext = application.run(args);
  applicationContext.close();
 }

}

When you ran ‘Application.java’, you can able to see below messages in the console.
********************************
Dev profile is called
********************************
********************************
Prod profile is called
********************************

Project structure looks like below.





Activate profiles by using Spring’s ConfigurableEnvironment interface
You can also activate the profile using 'ConfigurableEnvironment'.

@Autowired
private ConfigurableEnvironment env;
...
env.setActiveProfiles("someProfile");

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  SpringApplication application = new SpringApplication(Application.class);

  ConfigurableEnvironment environment = new StandardEnvironment();
  environment.setActiveProfiles("dev", "prod");

  application.setEnvironment(environment);

  ConfigurableApplicationContext applicationContext = application.run(args);
  
  applicationContext.close();
 }

}







Previous                                                 Next                                                 Home

No comments:

Post a Comment