Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Using spring.profiles.include for Flexible Environment Configuration in Spring Boot

Tech Jul 1 2

In real-world deployments, Spring Boot applications often run across multiple environments such as development, testing, and production. Each environment typically requires distinct settings—like database URLs, port numbers, or external service endpoints. Manually editing configuraton files per environment before packaging is error-prone and inefficient.

Spring Boot offers an elegant solution for environment-specific configuration through profile-based property files. These files follow the naming convention application-{profile}.properties, where {profile} identifies the environment—e.g.,

  • application-dev.properties for development,
  • application-test.properties for testing,
  • application-prod.properties for production.

A specific profile is activated via the spring.profiles.active property. For instance, setting spring.profiles.active=test in application.properties loads application-test.properties.

Core Mechanism: Profile-Specific Properties

To illustrate, suppose each environment uses a unique server port:

  • application-dev.properties: server.port=8080
  • application-test.properties: server.port=9090
  • application-prod.properties: server.port=80

The default profile is set in application.properties as:

spring.profiles.active=dev

When launching the application:

  • java -jar app.jar → Port 8080 (dev environment)
  • java -jar app.jar --spring.profiles.active=test → Port 9090
  • java -jar app.jar --spring.profiles.active=prod → Port 80

In CentOS, a typical deployment command might be:

java -jar -Dloader.path=./lib \
       -Dspring.profiles.active=test \
       -Xms128m -Xmx512m app.jar

Note the precedence order for profile activation: Command-line argument > -D JVM flag > System environment variables > Static config files

Activating Multiple Profiles

Multiple profiles can be active simultaneously by separating them with commas:

--spring.profiles.active=dev,test

This merges configurations, with later profiles overriding earlier ones where conflicts occur.

spring.profiles.include for Modular Overlays

Sometimes, you need reusable shared configuration layers—for example, common logging or monitoring settings applied across all environment, in addition to the active profile. The spring.profiles.include directive enables this:

In YAML format (application.yml):

spring:
  profiles:
    include:
      - logging
      - monitoring

Or comma-separated:

spring:
  profiles:
    include: logging,monitoring

This implicitly activates the logging and monitoring profiles, loading application-logging.yml and application-monitoring.yml, which are then combined with the active profile’s settings.

⚠️ Spring Boot 2.4+ Compatibility NoteIn Spring Boot 2.4 and latter, the legacy processing model for profiles was deprecated. To retain the above behavior using spring.profiles.include, either:

  1. Enable legacy mode:

    spring:
      config:
        use-legacy-processing: true
    
    
  2. Or adopt the newer spring.config.activate.on-profile syntax (recommended for new projects):

    spring:
      config:
        activate:
          on-profile: dev
    
    

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.