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.
Why Do We Use Profiles?
Different environments often require different configurations.
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=8083Only 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);
}
}
}dev
or
prod
or
dev
testdepending 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=devspring.profiles.active=dev,test
java -jar app.jar --spring.profiles.active=prod
SPRING_PROFILES_ACTIVE=prod
spring.profiles.active=prod
environment.getActiveProfiles()
["prod"]
This confirms that production-specific configurations are currently loaded.
Benefits
Real-World Example
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.
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?