Spring Boot Profiles for Environment Management

Managing different environments (development, testing, production) is a common requirement in application development. Spring Boot Profiles simplify this by enabling developers to configure environment-specific properties, beans, and behavior. This feature eliminates the need for manual configuration changes when deploying your application across various stages. By using profiles like dev, prod, and test, you can control how your application behaves in each environment.

This guide explains how to use Spring Boot profiles effectively, including setting up profile-specific configuration files, activating profiles, defining beans per profile, and real-world usage.

Table of Contents

  1. What Are Spring Boot Profiles?
  2. Using application-dev.yml and application-prod.yml
  3. Setting Active Profiles
  4. Profile-Specific Beans and Configurations
  5. Real-World Scenarios
  6. External Resources for Further Learning
  7. Final Thoughts

What Are Spring Boot Profiles?

Spring Boot Profiles provide a mechanism to separate configuration and behavior for different environments. You can define multiple profiles (e.g., dev, prod, test) and Spring Boot will automatically load the configuration files and beans associated with the active profile.

Benefits

  • Environment-Specific Configurations: Use different configurations for development, testing, and production environments without mixing them.
  • Simplified Deployment: Avoid manual intervention during deployment by predefining properties for each environment.
  • Clean Codebase: Keep configurations context-specific and avoid cluttering your main configuration file.

Using application-dev.yml and application-prod.yml

Spring Boot allows you to create profile-specific configurations using property or YAML files. These files are automatically loaded based on the active profile.

Default Configuration File

The default configuration (application.properties or application.yml) contains common properties applicable to all profiles. Specific profiles will override these properties as needed.

Profile-Specific Configuration

To define profile-specific configurations, create files like:

  • application-dev.yml (for development)
  • application-prod.yml (for production)

Example Configuration Files

application.yml (Default):

spring:
  application:
    name: my-app
server:
  port: 8080

application-dev.yml (Development):

server:
  port: 8081
spring:
  datasource:
    url: jdbc:h2:mem:devdb
    username: sa
    password:

application-prod.yml (Production):

server:
  port: 8082
spring:
  datasource:
    url: jdbc:mysql://prod-db-server/mydb
    username: prod-user
    password: prod-password
logging:
  level:
    org.springframework: WARN

How It Works

When the dev profile is active, Spring Boot loads application-dev.yml and merges it with the default configuration. For example:

  1. The server.port property will be overridden to 8081 for development.
  2. The common property spring.application.name remains in effect unless explicitly overridden.

Setting Active Profiles

You can set the active profile(s) in various ways, depending on your deployment scenario.

1. Using application.properties or application.yml

Add the following property to your default configuration file:

spring.profiles.active=dev

This will activate the dev profile.

2. Command-Line Arguments

Pass the profile as a --spring.profiles.active argument when running the application:

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

3. Environment Variables

Set the profile as an environment variable before starting your application:

export SPRING_PROFILES_ACTIVE=prod

4. IDE Configuration

When using an IDE like IntelliJ or Eclipse, you can set active profiles in the Run Configurations:

  • IntelliJ IDEA: Go to Run -> Edit Configurations -> VM Options and add -Dspring.profiles.active=dev.
  • Eclipse: Set the SPRING_PROFILES_ACTIVE environment variable in the launch configuration.

Multiple Profiles

You can activate multiple profiles by separating them with commas:

spring.profiles.active=dev,test

Spring Boot will load the configurations sequentially, where properties defined later override earlier ones.


Profile-Specific Beans and Configurations

Spring Boot lets you define beans and configurations specific to each profile using the @Profile annotation.

Profile-Based Beans

Define beans that are only loaded for a specific profile:

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        return DataSourceBuilder.create()
                                .url("jdbc:h2:mem:devdb")
                                .username("sa")
                                .build();
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        return DataSourceBuilder.create()
                                .url("jdbc:mysql://prod-db-server/mydb")
                                .username("prod-user")
                                .password("prod-password")
                                .build();
    }
}

Conditional Annotations

Use @ConditionalOnProperty to load beans conditionally based on properties:

@Bean
@ConditionalOnProperty(name = "spring.profiles.active", havingValue = "prod")
public CacheService cacheService() {
    return new CacheServiceImpl();
}

Testing Profiles

For test environments, Spring provides a dedicated test profile. You can create a separate application-test.yml file for test-specific settings. This is particularly useful for setting up in-memory databases (e.g., H2) during unit testing.


Real-World Scenarios

1. Database Configurations

Use different databases for development and production:

  • Development: H2 or another in-memory database.
  • Production: MySQL, PostgreSQL, etc.

Example (application-dev.yml):

spring.datasource.url=jdbc:h2:mem:testdb

Example (application-prod.yml):

spring.datasource.url=jdbc:mysql://prod-db-server/testdb

2. Logging Levels

Set more verbose logging in development and less in production:

  • Development: DEBUG logging for troubleshooting.
  • Production: WARN or ERROR level logging to reduce output.

Example (application-dev.yml):

logging.level.org.springframework=DEBUG

Example (application-prod.yml):

logging.level.org.springframework=ERROR

3. External APIs

Point to different API endpoints based on the profile:

# application-prod.yml
external.api.url=https://api.production.com
# application-dev.yml
external.api.url=https://api.staging.com

4. Feature Toggles

Enable or disable features using profiles:

# application-dev.yml
feature.toggle.experimental=true
# application-prod.yml
feature.toggle.experimental=false

External Resources for Further Learning


Final Thoughts

Spring Boot Profiles provide a powerful mechanism for managing environment-specific configurations and behaviors. By organizing properties, beans, and application logic per profile, you ensure smoother development, testing, and deployment workflows.

This guide has equipped you with the fundamentals of profiles, from basic YAML configurations to real-life examples. Use Spring Boot Profiles to their full potential and optimize your application’s deployment processes. Bookmark this guide and revisit it whenever you manage environment configurations!

Similar Posts

Leave a Reply