How can you programmatically determine which profiles are currently active in a Spring Boot application?

Spring Boot provides the Environment interface to access application configuration details, including the profiles that are currently active. By using the getActiveProfiles() method, developers can programmatically determine which profile-specific configurations are loaded and being used by the application at runtime.

Key Points

Why Do We Use Profiles?

Different environments often require different configurations.

Examples

Development

Testing

Production

Profiles allow Spring Boot to load environment-specific configurations automatically.

Example Configuration

application-dev.properties

server.port=8081

application-test.properties

server.port=8082

application-prod.properties

server.port=8083

Only the configuration associated with the active profile will be loaded.

How to Retrieve Active Profiles?

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

@Component
public class ProfileChecker {

    @Autowired
    private Environment environment;

    public void displayProfiles() {

        String[] activeProfiles =
                environment.getActiveProfiles();

        for (String profile : activeProfiles) {

            System.out.println(profile);
        }
    }
}

Output Example

dev

or

prod

or

dev
test

depending on the active profiles.

How It Works Internally

Application Starts

|

Profiles Activated

|

Environment Created

|

getActiveProfiles()

|

Returns Active Profiles

Spring stores profile information inside the Environment object during startup.

Checking if a Specific Profile is Active

if (environment.acceptsProfiles("prod")) {

    System.out.println(
            "Production Profile Active");
}

This is useful when application behavior needs to change based on the current environment.

Displaying Profiles at Startup

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class StartupRunner
        implements CommandLineRunner {

    private final Environment environment;

    public StartupRunner(
            Environment environment) {

        this.environment = environment;
    }

    @Override
    public void run(String... args) {

        String[] profiles =
                environment.getActiveProfiles();

        for (String profile : profiles) {

            System.out.println(
                    "Active Profile: " + profile);
        }
    }
}

This prints all active profiles when the application starts.

How to Activate Profiles?

application.properties

spring.profiles.active=dev

Multiple Profiles

spring.profiles.active=dev,test

Command Line

java -jar app.jar --spring.profiles.active=prod

Environment Variable

SPRING_PROFILES_ACTIVE=prod

Example

Suppose an application has three profiles

Current configuration

spring.profiles.active=prod

Calling

environment.getActiveProfiles()

returns

["prod"]

This confirms that production-specific configurations are currently loaded.

Benefits

Real-World Example

A banking application uses

dev

test

prod

At startup, the application checks the active profile and loads the appropriate configuration automatically. Developers can verify the active environment using the Environment interface.

Interview Tip

A concise interview answer is

We can determine the active profiles in a Spring Boot application by injecting the Environment interface and calling the getActiveProfiles() method. It returns an array containing the names of all currently active profiles, allowing us to identify which environment-specific configurations are being used at runtime.

Last updated: 16 July 2026

Previous question: What is the difference between @SpringBootApplication and @EnableAutoConfiguration?

Next question: What is the difference between deploying a WAR file and using an embedded container?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions