Spring Boot Inherit application.properties from dependency

后端 未结 2 985
面向向阳花
面向向阳花 2020-12-01 09:25

Let\'s say I have 5 Spring Boot Projects. All of them have a Maven dependency on a Spring Boot project No 6 with some shared/common classes. 5 independent projects have a lo

相关标签:
2条回答
  • 2020-12-01 09:58

    Use PropertySource annotation and provide two sources for your app:

    @PropertySources({
            @PropertySource("classpath:app-common.properties"),
            @PropertySource("classpath:app.properties")
        })
    

    more details can be found there https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

    0 讨论(0)
  • 2020-12-01 10:00

    Currently spring boot doesn't provide inheriting properties.

    A spring boot application supports multiple property sources but the convention (read: built-in logic) for xxx.properties is to resolve the last xxx.properties if there are multiple properties files having the same file name.

    There are many solution to this.

    One possible solution is to

    1. apply a custom profile to the dependency
    2. include inheritable settings in the application-customprofile.properties
    3. have the dependent(s) set spring.profiles.include=customprofile in application[-{profile}].properties (note: if set in application.properties, it applies for all profiles)

    Another possible solution is to use a unique custom filename for the properties.

    • i.e. instead of using the default application.properties, use common.properties
    0 讨论(0)
提交回复
热议问题