'How to override a property after importing few property files in application yml in springboot 2.4

I have two property files in yml

  1. application-common.yml with below content

    web-client:
      max-connections: 500
    wait-for:
      time: 1s
    
  2. application.yml with below content

    info:
      app:
        name: "@project.name@"
        version: "@project.version@"
    spring:
      application.name: "@project.name@"
    
    ---
    spring:
      config:
        activate.on-profile: local
        import: classpath:application-common.yml
    wait-for:
      time: 10s
    
    ---
    spring:
      config:
        activate.on-profile: prod
        import: classpath:application-common.yml
    wait-for:
      time: 5s
    

I was expecting that I should be able to override wait-for property after importing application-common.yml to 10s for the local profile and 5s for the prod profile. But when I run the application with local profile, the wait-for property is resolved to 1s instead of 10s



Solution 1:[1]

I'm not very sure about this response since I haven't testet it, but I think the standard layout should rather be as follows:

  • application.yml <= shared/common stuff
  • application-local.yml <= local profile specific overrides
  • application-prod.yml <= prod profile specific overrides

At least, that's how I would intuitively do it to follow best practices and would expect it to work.

Solution 2:[2]

You can override the property by redefining it in another yml or properties file. But make sure that, which ever property file gets read at last, those properties will be effective/considered in the application.

e.g. if your common yml file gets read last, then "wait-for" will be 1s & if application yml file gets read last, then "wait-for" will be 10s.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dagnelies
Solution 2 Abhale Amol