Search code examples
javaspring-bootspring-boot-configuration

Conditionally replace spring boot config yml property


I would like to replace a configuration yml property using a condition based on environment variables:

spring:
  datasource:
    username:${ENV} == 'PROD' ? ${USER_PROD} : ${USER_TEST}
    password: ${ENV} == 'PROD' ? ${PWD_PROD} : ${PWD_PROD}

Is there any way I can do this inside my application.yml or programmatically?

I have not faced this situation before


Solution

  • The normal way of doing this is to use different application.properties file each representing a "profile".

    Then you can override the desired properties based on the profile and run the application using that profile using -Dspring.profiles.active.

    A useful guide on the following link.