Search code examples
javaspringspring-bootconfiguration-files

Remove key from default profile yml in Spring Boot


I have a Spring Boot application.yml file structured as:

mapValues:
    key1: value1
    key2: value2

Now I want to run the application in another profile where those key/value should be replaced with different keys in application-profile.yml as:

mapValues:
    key3: value3
    key4: value4

Spring seems to be merging these two yml profiles automatically and the application sees:

mapValues:
    key1: value1
    key2: value2
    key3: value3
    key4: value4

I do not want to keep key1 and key, instead just keep key3 and key4. How can this be achieved?


Solution

  • What you specify in your application.yml file defaults to all profiles. If you want to separate properties, create multiple yml files, like application-dev.yml and application-test.yml and specify the profile explicitly like below (typically in bootstrap.yml)

    spring:
      profiles:
        active: test
    

    With the above profile set, your app will see the properties in application.yml and application-test.yml files only. Properties in application-dev.yml will not be available.