如何在Spring中继承application.properties?

mem*_*und 12 java spring 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>
Run Code Online (Sandbox Code Playgroud)

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

mem*_*und 8

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

在共享库中:

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

在主应用程序中:

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

}
Run Code Online (Sandbox Code Playgroud)

通过这种方式可以加载commons / shared配置,但是可以在主应用程序的application.properties中覆盖它。

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

  • 请注意,这仅适用于.properties文件,不适用于.yml文件。/sf/answers/2938909571/ (2认同)