属性文件的spring boot外部配置

新手上路,请多包涵

我有一个 spring boot 应用程序,我可以将它打包到我想部署到不同环境的战争中。要使此部署自动化,将配置文件外部化会更容易。

目前在 src/main/resources 中的 application.properties 文件一切正常。然后我使用“mvn install”构建一个可部署到 tomcat 的 war。但我想使用一个 .yml 文件,它不需要出现在 mvn install 上,但可以在部署战争期间从中读取,并且与我的战争位于同一目录或相对目录中。

24. 外部配置 显示 spring boot 将在何处查找文件, 72.3 更改应用程序外部属性的位置 提供了有关如何配置的更多详细信息,但我只是不明白如何将其转换为我的代码。

我的应用程序类如下所示:package be.ugent.lca;

 Updated below

我需要在这个文件中添加一个@PropertySource 吗?我将如何引用某个相对路径?

我觉得它可能作为大多数 spring boot 文档记录在那里,但我只是不明白他们是如何让我这样做的。

编辑

不确定这是否应该是一个单独的问题,但我认为它仍然相关。

设置 os 变量后,找不到 yaml 文件的错误消失了。然而,当我没有应用程序 .properties 或 .yml 文件时,我仍然再次遇到同样的错误。应用程序现在看起来像这样:

 @Configuration
**@PropertySource("file:${application_home}/application.yml")**
@ComponentScan({"be.ugent.lca","be.ugent.sherpa.configuration"})
@EnableAutoConfiguration
@EnableSpringDataWebSupport
public class Application extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }

application_home 操作系统变量

$ echo $application_home
C:\Masterproef\clones\la15-lca-web\rest-service\target

我的 application.yml 文件(它抱怨的部分):

 sherpa:
  package:
    base: be.ugent.lca

java -jar *.war 时出错 所有变化发生在:

 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'sherpa.package.base' in string value "${sherpa.package.base}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 142 more

原文由 turoni 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 685
2 个回答

使用外部属性文件

答案就在 Spring Boot 文档中,我会尽力为您分解。

首先,不,你不应该使用 @PropertySource 在使用 Yaml 配置时,如 Yaml 缺点 下所述:

无法通过 @PropertySource 注释加载 YAML 文件。因此,如果您需要以这种方式加载值,则需要使用属性文件。

那么,如何加载属性文件呢?这在此处解释 应用程序属性文件

为您加载了一个: application.yml ,将其放在上面链接中提到的目录之一中。这非常适合您的一般配置。

现在对于您想要使用外部属性文件的环境特定配置(以及密码之类的东西),该部分还解释了如何执行此操作:

如果您不喜欢 application.properties 作为配置文件名,您可以通过指定 spring.config.name 环境属性来切换到另一个。您还可以使用 spring.config.location 环境属性(以逗号分隔的目录位置列表或文件路径)来引用显式位置。

所以你使用 spring.config.location 环境属性。假设您有一个外部配置文件: application-external.yml 在您的主目录下的 conf/ 目录中,只需像这样添加它: -Dspring.config.location=file:${home}/conf/application-external.yml 作为您的 JVM 的启动参数。如果您有多个文件,只需用逗号分隔它们。请注意,您可以轻松地使用这样的外部属性来覆盖属性,而不仅仅是添加它们。

我建议通过让您的应用程序仅使用内部 application.yml 文件来测试这一点,然后覆盖外部属性文件中的(测试)属性并将其值记录在某处。

将 Yaml 属性绑定到对象

在使用 Yaml 属性时,我通常使用 @ConfigurationProperties 加载它们,这在处理例如列表或更复杂的属性结构时非常有用。 (这就是您应该使用 Yaml 属性的原因,对于简单的属性,您最好使用常规属性文件)。阅读此内容以获取更多信息: 类型安全配置属性

额外:在 IntelliJ、Maven 和 JUnit 测试中加载这些属性

有时您希望在 Maven 构建中或执行测试时加载这些属性。或者只是为了使用您的 IDE 进行本地开发

如果您使用 IntelliJ 进行开发,您可以通过将其添加到您的 Tomcat 运行配置来轻松添加它:“运行”->“编辑配置”,在“Tomcat 服务器”下选择您的运行配置,检查服务器选项卡并将其添加到“VM”下选项”。

要在 Maven 构建 中使用外部配置文件:在 pom.xml 中像这样配置 Maven surefire 插件:

 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
     <argLine>-Dspring.config.location=file:${home}/conf/application-external.yml</argLine>
   </configuration>
</plugin>

在 IntelliJ 中运行 JUnit 测试 时:

  • 运行 → 编辑配置
  • 默认值 → JUnit
  • 添加虚拟机选项 -> -ea -Dspring.config.location=file:${home}/conf/application-external.yml

原文由 loïc 发布,翻译遵循 CC BY-SA 4.0 许可协议

是的,您需要使用 @PropertySource 如下所示。

这里的重点是 您需要提供 application_home 属性(或选择任何其他名称)作为操作系统环境变量或系统属性,或者您可以在启动 Spring 引导时作为命令行参数传递。此属性告诉配置文件( .properties.yaml )的确切位置(示例: /usr/local/my_project/

@Configuration
@PropertySource("file:${application_home}config.properties")//or specify yaml file
@ComponentScan({"be.ugent.lca","be.ugent.sherpa.configuration"})
@EnableAutoConfiguration
@EnableSpringDataWebSupport
public class Application extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

原文由 developer 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题