【问题标题】:Spring Boot - configure properties of a jar included as dependency into another jarSpring Boot - 配置作为依赖项包含在另一个 jar 中的 jar 的属性
【发布时间】:2021-01-15 02:10:42
【问题描述】:

我有一个 Spring Boot Web 应用程序 A,它依赖于一个 Spring Boot jar 库 B。我有一些我想在 B 中配置的属性,并且不希望客户端应用程序(例如 Web 应用程序 A)配置他们。我在 B 中有这些属性文件。

application-dev.properties

application-stage.properties

applicatino-live.properties

问题是,当库作为依赖项添加到 Web 应用 A 中时,这些属性无法识别。实现这一点的方法是什么?

【问题讨论】:

  • 如果这些是 2 个不可能的 Spring Boot 应用程序。
  • 是的,它们是 Spring Boot 应用程序
  • 那么由于Spring Boot fat jar的特殊结构,它不会起作用。

标签: spring spring-boot properties


【解决方案1】:

PropertySource 注解可以用在 webapp A 的 Configuration 类中来实现你想要的:

@Configuration
@PropertySource("classpath:application-dev.properties"),
@PropertySource("classpath:application-stage.properties"),
@PropertySource("classpath:application-live.properties")
public class WebappAConfiguration {
    
}

我还发现 spring boot 查找外部配置的顺序很有帮助 here

【讨论】:

  • 像这样使用spring.profiles.active 是个坏主意。它是一个String[],可以容纳多个配置文件,然后会中断。如果存在,Spring Boot 将自动从类路径中加载这些文件。
  • 啊@M.Deinum 你说得对,我删除了那个建议。
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
相关资源
最近更新 更多