【问题标题】:Properties hierarchy in spring boot project with dependency具有依赖关系的 Spring Boot 项目中的属性层次结构
【发布时间】:2020-12-21 15:28:20
【问题描述】:

我有一个常见的项目,带有弹簧配置类

@Configuration
@ImportResource(locations = {"classpath*:spring/spring-test-context.xml"})
public class CommonConfiguration{
}

在这个 spring-test-context.xml 中有一些 bean 需要从属性文件中注入值。

我在几个项目中使用这个通用库,但这些属性并非在所有项目中都使用。

所以我将它们与 null 值放在这个公共库的 application.properties 文件中,但它们在给定项目中从那里不可见,所以我需要在每个项目中的 application.properties 文件中添加 null 值,这些项目依赖于公共库.

我想在公共库中使用默认值的这些属性,并在需要它们的项目中用特定值覆盖?

怎么做?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您应该能够通过公共库 application.properties 提供 PropertySource 来实现您的目标。也许将其重命名为 spring-test-context.properties 只是为了确保与标准属性文件没有混淆:

    @Configuration
    @PropertySource("classpath:spring-test-context.properties")
    @ImportResource(locations = {"classpath*:spring/spring-test-context.xml"})
    public class CommonConfiguration{
    }
    

    【讨论】:

    • 谢谢,这是我需要的解决方案
    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多