Saturday 4 August 2018

Spring boot: How to get all active profiles

Below statements return all the active profiles.

Environment environment = applicationContext.getBean(Environment.class);
for (final String profileName : environment.getActiveProfiles()) {
         System.out.println("Currently active profile : " + profileName);
}

Find the below working application.

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.Environment;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Environment environment = applicationContext.getBean(Environment.class);
  for (final String profileName : environment.getActiveProfiles()) {
   System.out.println("Currently active profile : " + profileName);
  }

  applicationContext.close();
 }

}



Previous                                                 Next                                                 Home

No comments:

Post a Comment