spring boot 属性文件的外部配置 [英] Spring boot external configuration of property file

查看:54
本文介绍了spring boot 属性文件的外部配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a spring boot application that I can package in a war that I want to deploy to different environments. To automate this deployment it'd be easier to have the configuration file externalized.

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

Currently everything works fine with a application.properties file in src/main/resources. Then I use ´mvn install´ to build a war deployable to tomcat. But I would like to use a .yml file that does not need to be present on mvn install but that would be read from during deployment of the war and is in the same or a directory relative to my war.

24.外部化配置 显示了 spring boot 将在哪里查找文件和 72.3 Change the location of external properties of an application 提供了有关如何配置的更多详细信息,但我只是不明白如何将其转换为我的代码.

24. externalized configuration shows where spring boot will look for files and 72.3 Change the location of external properties of an application gives more detail on how to configure this but I just do not understand how to translate this to my code.

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

My application class looks like this: package be.ugent.lca;

Updated below

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

Do I need to add a @PropertySource to this file? How would I refer to a certain relative path?

我觉得它可能像大多数 Spring Boot 文档一样记录在那里,但我只是不明白他们是如何让我这样做的.

I feel like it's probably documented in there as most spring boot documentation but I just don't understand how they mean me to do this.

编辑

不确定这是否应该是一个单独的问题,但我认为它仍然相关.
设置 os 变量后,找不到 yaml 文件的错误消失了.然而,当我没有应用程序 .properties 或 .yml 文件时,我仍然再次遇到相同的错误.应用程序现在看起来像这样:

Not sure if this should be a separate issue but I think it's still related.
Upon setting the os variable the error of yaml file not found went away. Yet I still get the same error again as when I had no application .properties or .yml file. Application now looks like this:

@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 操作系统变量

The application_home OS variable

$ echo $application_home  
C:Masterproefclonesla15-lca-web
est-service	arget

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

My application.yml file(part it complains about):

sherpa:
  package:
    base: be.ugent.lca

java -jar *.war 出错所有变体:

Error upon java -jar *.war All variations upon:

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

推荐答案

使用外部属性文件

答案在 Spring Boot Docs 中,我会尽力为您分解.

The answer lies in the Spring Boot Docs, I'll try to break it down for you.

首先,在使用 Yaml 配置时不应该使用 @PropertySource,正如这里在 Yaml 缺点 :

First of all, no you should not use @PropertySource when working with Yaml configuration, as mentioned here under the Yaml shortcomings :

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

YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.

那么,如何加载属性文件?这里解释了 应用程序属性文件

So, how to load propery files? That is explained here Application Property Files

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

One is loaded for you: application.yml , place it in one of the directories as mentioned in the link above. This is great for your general configuration.

现在对于您要使用外部属性文件的环境特定配置(以及诸如密码之类的东西),该部分还解释了如何做到这一点:

Now for your environment specific configuration (and stuff like passwords) you want to use external property files, how to do that is also explained in that section :

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

If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).

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

So you use the spring.config.location environment property. Imagine you have an external config file: application-external.yml in the conf/ dir under your home directory, just add it like this: -Dspring.config.location=file:${home}/conf/application-external.yml as a startup parameter of your JVM. If you have multiple files, just seperate them with a comma. Note that you can easily use external properties like this to overwrite properties, not just add them.

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

I would advice to test this by getting your application to work with just your internal application.yml file , and then overwrite a (test) property in your external properties file and log the value of it somewhere.

将 Yaml 属性绑定到对象

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

When working with Yaml properties I usually load them with @ConfigurationProperties, which is great when working with for example lists or a more complex property structure. (Which is why you should use Yaml properties, for straightforward properties you are maybe better of using regular property files). Read this for more information: Type-Safe Configuration properties

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

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

Sometimes you want to load these properties in your maven builds or when performing tests. Or just for local development with your IDE

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

If you use IntelliJ for development you can easily add this by adding it to your Tomcat Run Configuration : "Run" -> "Edit Configurations" , select your run configuration under "Tomcat Server" , check the Server tab and add it under "VM Options".

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

To use external configuration files in your Maven build : configure the maven surefire plugin like this in your pom.xml:

<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 测试时:

When running JUnit tests in IntelliJ:

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

这篇关于spring boot 属性文件的外部配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆