Testing the Active Profile in a Spring Application

The first thing to do is @Inject the Spring Environment into your own Spring bean.

@Inject
private Environment environment;

Now you can ask the environment whether a given profile is active or not:

if (Arrays.asList(environment.getActiveProfiles()).contains("dev")) {
  LOG.info("Profile 'dev' is active");
  ...
}

A helpful colleague pointed out that this is a bit cumbersome and suggested to use the more readable form:

if (env.acceptsProfiles("dev")) {
  LOG.info("Profile 'dev' is active");
  ...
}
Show Comments