Skip to main content

Spring : How to get Current Profiles in Spring Application

Points To Remember

You can use the Environment interface in package org.springframework.core.env to get the current profiles in spring application.

You can make use of the following methods to get the information about the profiles.

  • acceptsProfiles(String ...profiles) - returns if the profiles given in input is active.
  • getActiveProfiles() - Gives a list of profiles explicitly made active for this enviornment.
  • getDefaultProfiles() - gives a set of profiles to be active by default if no active profile is set explicitly.
See how to set and configure profiles in spring.

Must Read :: What are profiles in Spring boot.

How to get Active Profiles in an Environment

All you need to do is

  • autowire the Environmnet bean.
  • call the getActiveProfiles method of the bean.
package com.ekiras.config;

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

@Component
class Test {

@Autowired private Environment environment;

public void test(){
for(String profile : environment.getActiveProfiles()){
System.out.println(">>>>>>"+profile);
}
}

}
When the above method is called it will give the following output.
>>>>>>dev

Comments