Spring Boot
Active Profiles
Programming
Duplicate
Java

How to get the active profile programmatically in Spring Boot

Codemia Knowledge Hub ·

Inject Spring's Environment and call environment.getActiveProfiles(), which returns the active profiles as a String[]. You can also log the profiles at startup with an ApplicationListener, or inject spring.profiles.active with @Value. Profiles hold environment-specific configuration such as dev and prod, and checking which one is active helps with debugging, logging and conditional configuration.

How Spring profiles work

Spring Profiles allow you to define different beans and configurations for different environments. A typical use case is having a dev profile for development, and a prod profile for production, each with distinct configurations.

Activating Profiles

Profiles are activated via:

  • Properties Files: Define in application.properties or application.yml.
  • Command-Line Arguments: Use --spring.profiles.active.
  • Environment Variables: Set SPRING_PROFILES_ACTIVE.

Example configuration

Each profile can have its own properties file, such as application-dev.properties and application-prod.properties:

properties
# application-dev.properties
app.environment=development
properties
# application-prod.properties
app.environment=production

Reading the active profiles with Environment

Spring's Environment interface is the center of the configuration model and exposes both profiles and properties. Inject it and call getActiveProfiles():

java
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.core.env.Environment;
3import org.springframework.stereotype.Component;
4
5@Component
6public class ActiveProfileUtil {
7
8    @Autowired
9    private Environment environment;
10
11    public String[] getActiveProfiles() {
12        return environment.getActiveProfiles();
13    }
14}

Logging the active profiles at startup

An ApplicationListener can print the active profiles when the application starts, which helps with logging:

java
1import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
2import org.springframework.context.ApplicationListener;
3
4public class ActiveProfileLogger implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
5
6    @Override
7    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
8        String[] activeProfiles = event.getEnvironment().getActiveProfiles();
9        System.out.println("Active profiles: " + String.join(", ", activeProfiles));
10    }
11}

ApplicationEnvironmentPreparedEvent fires before the application context exists, so a listener declared as a bean never receives it. Register ActiveProfileLogger with SpringApplication.addListeners(...) or in META-INF/spring.factories.

Injecting the profile with @Value

You can also inject the property directly into a bean:

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3
4@Component
5public class ProfileService {
6
7    @Value("${spring.profiles.active:default}")
8    private String activeProfile;
9
10    public String getActiveProfile() {
11        return activeProfile;
12    }
13}

Which method to use

MethodDescriptionImplementation Example
Environment InterfaceAccess profiles via Spring's environment injection.environment.getActiveProfiles()
ApplicationListenerLog profiles during application startup.Implement listener for ApplicationEnvironmentPreparedEvent
@Value AnnotationDirectly inject active profile as a property.@Value("$&#123;spring.profiles.active:default&#125;")

Default profiles, multiple profiles and tests

Setting a default profile

When no profile is set, spring.profiles.default decides which one applies:

properties
# application.properties
spring.profiles.default=dev

Activating several profiles

Separate profiles with commas, for example to activate both dev and debug:

bash
--spring.profiles.active=dev,debug

Choosing profiles in integration tests

The @ActiveProfiles annotation sets which profiles are active while integration tests run:

java
1@ExtendWith(SpringExtension.class)
2@SpringBootTest
3@ActiveProfiles("test")
4public class MyServiceTests {
5    // Test cases here
6}

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms