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

问题描述

我有一个Spring启动应用程序,我可以将其打包在一个我要部署到不同环境的战争中。要自动执行此部署,可以更容易地将配置文件外部化。

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的war。
但是我想使用一个不需要出现在mvn install上的.yml文件,但是在战争部署期间可以读取该文件,并且它与我的战争相同或相同。

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.

显示spring引导将查找文件的位置和提供了有关如何配置此内容的更多详细信息,但我只是不明白如何将此翻译成我的代码。

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.

我的应用程序类如下所示:
package 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?

我觉得它可能记录在那里作为大多数弹簧启动文档,但我只是不明白它们对我来说意味着什么这样做。

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:\Masterproef\clones\la15-lca-web\rest-service\target

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

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

sherpa:
  package:
    base: be.ugent.lca

java -jar上的错误* .war
所有版本变化:

Error upon java -jar *.warAll 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 ,如:

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

那么,如何加载属性文件?这在这里解释

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 :

因此,您使用 spring.config.location 环境属性。
想象一下,你的主目录下的conf / dir中有一个外部配置文件: 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文件,然后覆盖外部属性文件中的(test)属性并将其值记录到某处。

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
   </configuration>
</plugin>

在IntelliJ中运行 JUnit测试时:

When running JUnit tests in IntelliJ:


  • 运行→编辑配置

  • 默认值→JUnit

  • 添加VM选项 - > -ea -Dspring.config.location = file:$ {home} /conf/application-external.yml

  • Run → Edit Configurations
  • Defaults → JUnit
  • add VM Options -> -ea -Dspring.config.location=file:${home}/conf/application-external.yml

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

08-15 04:53