Managing Environment Profiles in Spring Applications

Managing Enviroment Profiles in Spring Applications

When developing web applications, it's common to have different configurations for development, testing, and production environments. Spring provides a robust mechanism called profiles to manage these environment-specific configurations through the @Profile annotation.

Using the @Profile Annotation

The @Profile annotation in Spring allows you to mark beans that should only be registered when a specific profile is active. This annotation can be applied at both the class and method level.

Applying @Profile to Configuration Classes

When applied to a configuration class, all @Bean methods within that class will only be registered if the specifeid profile is active.

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.example.services.DataService;
import com.example.services.LoggingService;

@Configuration
@Profile("development")
public class DevelopmentConfiguration {
    
    @Bean
    public DataService dataService() {
        return new DevelopmentDataService();
    }
    
    @Bean
    public LoggingService loggingService() {
        return new DebugLoggingService();
    }
}

Applying @Profile to Individual Bean Methods

You can also apply the @Profile annotation directly to @Bean methods, allowing different beans from the same configuration class to be registered based on different profiles.

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.example.services.DataService;
import com.example.services.LoggingService;
import com.example.services.ProductionDataService;
import com.example.services.ErrorLoggingService;

@Configuration
public class ServiceConfiguration {
    
    @Bean
    @Profile("production")
    public DataService productionDataService() {
        return new ProductionDataService();
    }
    
    @Bean
    @Profile("development")
    public DataService developmentDataService() {
        return new DevelopmentDataService();
    }
    
    @Bean
    @Profile("development")
    public LoggingService debugLoggingService() {
        return new DebugLoggingService();
    }
    
    @Bean
    @Profile("production")
    public LoggingService errorLoggingService() {
        return new ErrorLoggingService();
    }
}

Activating Profiles

Spring determines which profiles to activate based on two properties: spring.profiles.active and spring.profiles.default. The spring.profiles.active property takes precedence over spring.profiles.default. If neither is set, no profiles are activated by default.

Multiple profiles can be activated by separating them with commas, such as: spring.profiles.active=dev,qa

Methods for Activating Profiles

  1. DispatcherServlet Initialization Parameters
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>production</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

  1. Web Application Context Parameters
<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>staging</param-value>
</context-param>

  1. JNDI Entries

  2. Environment Variables

Setting Environment Variables

Windows Systems:

set spring.profiles.active=development

Linux/Ubuntu Systems (current user):

$ echo 'export spring.profiles.active=development' >> ~/.bashrc
$ source ~/.bashrc

Linux/Ubuntu Systems (all users):

$ sudo echo 'export spring.profiles.active=development' >> /etc/profile
$ source /etc/profile

  1. JVM System Properties
-Dspring.profiles.active=testing

  1. @ActiveProfiles Annotation in Tests
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@ActiveProfiles("integration")
public class IntegrationTest {
    // test code
}

Best Practices

For web applications, it's recommended to:

  • Set a default profile (e.g., production) through context parameters
  • Use environment variables to override the active profile when needed
  • Keep environment-specific configurations separate
  • Use profile-specific properties files (application-{profile}.properties)

By implementing these practices, you can ensure smooth transitions between different environments while maintaining clean, manageable configuration.

Tags: Spring Framework Java Web Development Environment Configuration Profile Management

Posted on Wed, 20 May 2026 19:28:12 +0000 by kelesis