Spring Boot - Get Active Profiles Programmatically

This tutorial shows you how to get the list of active profiles in a Spring Boot application.

Spring Boot has a feature called profiles which allows an application to be run with different configurations. In another tutorial, I have written about how to run a Spring Boot application with profiles. When the application has been run, sometimes you may need to know the list of active profiles. For example, if you have a logic that depends on whether a profile is active or not. In this tutorial, I'm going to show you how to get all active profiles programmatically.

Using Environment

Spring has a class named Environment. It has a method named getActiveProfiles that returns an array of profiles that made active for the application. Therefore, you need to get the instance of Environment and call the method. The Environment object can be autowired in any Spring-managed components.

  @Service
  public class MyService {

    private final Environment environment;

    public MyService(Environment environment) {
      this.environment = environment;
    }

    public void test() {
      String[] actives = this.environment.getActiveProfiles();
    }
  }

The Environment class also has a method named acceptsProfiles by which you can check whether the Spring application is run with any of the given profiles. It returns true as long as there is a matched profile, despite the others being unmatched.

  boolean isAccepted = this.environment.acceptsProfiles(Profiles.of("staging", "worker"));

Another method of the Environment class that you may need to know is getDefaultProfiles which returns the default profiles to be used when there is no active one.

  String[] defaults = this.environment.getDefaultProfiles();

Get Profiles in Non-Bean Class.

Inside a Spring bean class, getting the Environment instance is quite simple since it can be autowired. However, if you need to get it inside a non-bean class, you need a little workaround.

The solution is, you can create a Spring bean class that implements EnvironmentAware. In that class, store the Environment in a static variable and create a getter for it.

  @Component
  public class EnvironmentProvider implements EnvironmentAware {

    private static Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
      EnvironmentProvider.environment = environment;
    }

    public static Environment getEnvironment() {
      return environment;
    }
  }

By doing that, it becomes possible to access it from any method.

  public class MyUtil {

    private MyUtil() {
      throw new AssertionError();
    }

    public static void test() {
      Environment environment = EnvironmentProvider.getEnvironment();
      String[] actives = environment.getActiveProfiles();
    }
  }

Using @Value Annotation

For profiles set from the spring.profiles.active property in an application.properties or application.yml file, it can be obtained by using the @Value annotation in a Spring bean. However, it doesn't include profiles set from other sources. Therefore, using Environment is the preferred way.

  @Value("${spring.profiles.active:}")
  private String activeProfiles;

Summary

The list of currently active profiles can be obtained from the Environment bean. You can get the list using the getActiveProfiles method, or check if there is any matching profile using the acceptsProfiles method. While some sources in the Internet recommend to use @Value annotation, it only works for profiles set using the application property.

You can also read about: