How to override application.properties during production in Spring-Boot?

后端 未结 8 732
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 13:00

I\'m using spring boot and application.properties to select a database during development by @Configuration @Profile(\"dev\").

spri         


        
相关标签:
8条回答
  • 2020-12-04 13:52

    I know you asked how to do this, but the answer is you should not do this.

    Instead, have a application.properties, application-default.properties application-dev.properties etc., and switch profiles via args to the JVM: e.g. -Dspring.profiles.active=dev

    You can also override some things at test time using @TestPropertySource

    Ideally everything should be in source control so that there are no surprises e.g. How do you know what properties are sitting there in your server location, and which ones are missing? What happens if developers introduce new things?

    Spring Boot is already giving you enough ways to do this right.

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

    0 讨论(0)
  • 2020-12-04 13:56

    The spring configuration precedence is as follows.

    1. ServletConfig init Parameter
    2. ServletContext init parameter
    3. JNDI attributes
    4. System.getProperties()

    So your configuration will be overridden at the command-line if you wish to do that. But the recommendation is to avoid overriding, though you can use multiple profiles.

    0 讨论(0)
提交回复
热议问题