Sunday, May 25, 2025

SpringBoot application properties

 What happens when you have default yaml or properties file along with the profile specific yml or properties files. eg: you have application.yml and application-dev.yml, application-test.yml, application-prod.yml


1. Default Behavior (No Active Profile)

  • Only application.yml is loaded

  • All other profile-specific files (-dev.yml-test.yml-prod.yml) are ignored

  • Useful for shared configurations (e.g., app name, default server port)


2. When a Profile is Activated

If you activate a profile (e.g., dev):

  1. application.yml is loaded first (base config)

  2. application-dev.yml is merged on top, overriding matching properties

  3. Other profile files (-test.yml-prod.yml) are ignored

Example:

yaml
Copy
Download
# application.yml (shared)
server:
  port: 8080
logging:
  level:
    root: INFO

# application-dev.yml (dev-only)
logging:
  level:
    root: DEBUG
    org.hibernate.SQL: TRACE

Result (--spring.profiles.active=dev):

yaml
Copy
Download
server:
  port: 8080  # From base
logging:
  level:
    root: DEBUG       # Overridden by dev
    org.hibernate.SQL: TRACE  # Added by dev

3. Key Rules

RuleExplanation
Profile-specific files override baseapplication-dev.yml wins over application.yml
Last profile winsIf multiple profiles are active (dev,test), the rightmost profile's config takes precedence
Non-conflicting properties mergeUnique properties from all files are combined
spring.profiles.includeForce-include additional profiles (e.g., spring.profiles.include: db,security)

4. How to Activate Profiles

Option 1: Command Line

sh
Copy
Download
java -jar app.jar --spring.profiles.active=prod

Option 2: Environment Variable

sh
Copy
Download
export SPRING_PROFILES_ACTIVE=test  # Linux/macOS
set SPRING_PROFILES_ACTIVE=test     # Windows

Option 3: In application.yml

yaml
Copy
Download
spring:
  profiles:
    active: dev  # Default profile (not recommended for production)

5. Advanced: Multi-Profile Activation

You can activate multiple profiles (e.g., dev + db-mysql):

sh
Copy
Download
--spring.profiles.active=dev,db-mysql

Spring Boot merges them in order (rightmost profile wins conflicts).


6. Best Practices

  1. application.yml → Store default/shared configs

  2. application-{profile}.yml → Store environment-specific overrides

  3. Never commit secrets → Use application-{profile}.yml with Vault/Env vars for prod credentials

  4. Use spring.config.activate.on-profile (YAML only) to conditionally include sections:

    yaml
    Copy
    Download
    # In application.yml
    spring:
      config:
        activate:
          on-profile: "dev"
    logging:
      level:
        root: DEBUG

7. Debugging Configuration

Check which profiles are active and property sources:

sh
Copy
Download
# Enable debug mode
java -jar app.jar --debug

Look for:

Copy
Download
The following profiles are active: dev
Property Sources:
  - application.yml
  - application-dev.yml

Summary

  • Base + Profile-Specific: Configs merge, with profile files overriding defaults

  • Profile Activation: Controls which -{profile}.yml files are loaded

  • Order Matters: Last profile in a comma-separated list has highest priority

This system allows clean separation of environments while avoiding duplication.

No comments:

Post a Comment