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: • Active profiles can be retrieved using the Environment interface. • The getActiveProfiles() method returns all currently active profiles. • Profiles help manage different configurations for development, testing, and production environments. • Multiple profiles can be active at the same time. • Profile information can be used for conditional logic within the application.
Why Do We Use Profiles?
Different environments often require different configurations.
Examples:
Development
• Local database • Debug logging
Testing
• Test database • Mock services
Production
• Production database • Optimized settings
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?
Code Example:
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
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
Code Example:
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
Code Example:
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=devMultiple 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:
• dev • test • prod
Current configuration:
spring.profiles.active=prod
Calling:
environment.getActiveProfiles()
returns:
["prod"]
This confirms that production-specific configurations are currently loaded.
Benefits
• Easy environment detection • Environment-specific behavior • Better configuration management • Simplified deployment process • Improved application flexibility
Real-World Example
A banking application uses:
dev
• Local database
test
• Test environment services
prod
• Production database and security settings
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."