小编典典

如何在Spring中继承application.properties?

spring-boot

如果我创建一个具有application.properties定义通用配置的通用库。喜欢:

spring.main.banner-mode=off

如何将这些属性继承到另一个包含这些公共库的项目中?

Maven:

<project ...>
    <groupId>de.mydomain</groupId>
    <artifactId>my-core</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <!-- this one holds the common application.properties -->
            <groupId>my.domain</groupId>
            <artifactId>my-commons</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

我怎样才能继承从配置my-commonsmy-core


阅读 459

收藏
2020-05-30

共1个答案

小编典典

解决方案是使用其他名称包含共享属性,此处 application-shared.properties

在共享库中:

@SpringBootApplication
@PropertySource(ResourceUtils.CLASSPATH_URL_PREFIX + "application-shared.properties") //can be overridden by application.properties
public class SharedAutoConfiguration {
}

在主应用程序中:

@SpringBootApplication
@Import(SharedAutoConfiguration.class)
public class MainAppConfiguration extends SpringBootServletInitializer {

}

这样,就可以加载commons / shared配置,但可以在主应用程序的application.properties中覆盖它。

它不适用于该spring.main.banner-mode属性(不知道为什么),但是与所有其他属性一起运行都很好。

2020-05-30