Spring Boot
Active Profile
Programmatic Access
Java
Configuration

How to determine programmatically the current active profile using Spring boot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Spring Boot, a popular framework for creating standalone, production-ready applications, provides robust support for multiple runtime environments through the concept of "profiles." Profiles allow you to define different configurations for different environments, such as development, testing, and production. Determining which profile is currently active can be essential for debugging, logging, or configuring your application programmatically. In this article, we'll explore how you can determine the active profile in a Spring Boot application programmatically and examine some key concepts related to profiles.

Understanding Spring Profiles

Spring Boot's profile feature allows developers to define a set of configuration files that activate under specific conditions. Profiles help in segregating parts of your application configuration and bean definitions that should only be available in certain environments.

The key benefits of using profiles include:

  • Environment-Specific Configurations: Different databases or third-party services for testing vs. production.
  • Conditional Bean Registration: Activate different beans based on the profile in use.
  • Controlled Properties: Load only the properties relevant to the active environment, avoiding unnecessary configurations.

Accessing Active Profiles

Spring provides several ways to determine which profile(s) are active within your application. Let's look at some code-centric methods to access active profiles programmatically.

Using Environment Interface

The Environment interface is part of the Spring Framework and provides a way to access properties and profiles within the application context.

java
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.core.env.Environment;
3import org.springframework.stereotype.Component;
4
5@Component
6public class ProfileService {
7
8    @Autowired
9    private Environment environment;
10
11    public String[] getActiveProfiles() {
12        return environment.getActiveProfiles();
13    }
14
15    public boolean isProfileActive(String profileName) {
16        return Arrays.asList(environment.getActiveProfiles()).contains(profileName);
17    }
18}

Using ApplicationListener for ApplicationEnvironmentPreparedEvent

Spring Boot provides various events during the application lifecycle. You can listen for these events to hook into certain stages of the application startup process.

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

To register this listener, you can use it in the SpringApplication run method.

java
1public class Application {
2
3    public static void main(String[] args) {
4        SpringApplication app = new SpringApplication(Application.class);
5        app.addListeners(new ProfileApplicationListener());
6        app.run(args);
7    }
8}

Direct Access via ApplicationContext

If your component requires access to application profiles post-initialization, you can use the ApplicationContext.

java
1import org.springframework.context.ApplicationContext;
2import org.springframework.context.ApplicationContextAware;
3import org.springframework.stereotype.Component;
4
5@Component
6public class ProfileAwareComponent implements ApplicationContextAware {
7
8    @Override
9    public void setApplicationContext(ApplicationContext applicationContext) {
10        String[] activeProfiles = applicationContext.getEnvironment().getActiveProfiles();
11        System.out.println("Active Profiles: " + String.join(", ", activeProfiles));
12    }
13}

Summary Table

These are three common methods to access active profiles programmatically in Spring Boot:

MethodDescription
Environment InterfaceProvides direct access to active profiles and properties within system components.
ApplicationEnvironmentPreparedEventHooks into the application lifecycle to capture profiles at an early startup stage.
ApplicationContextProvides a later lifecycle access point, useful for beans initialized after application startup.

Additional considerations:

  • Ordering: Ensure listener execution order by setting the @Order annotation.
  • Fallback: Use default profiles with environment.getDefaultProfiles() when no active profiles are set.
  • Profile-Specific Properties: Use properties prefixed with application-&#123;profile&#125;.properties.

By using these methods, you can dynamically access and manage active profiles, allowing for more flexible and adaptable application configurations.


Course illustration
Course illustration

All Rights Reserved.