Spring Boot 如何在 application.properties 中为 yaml 设置 spring.config.location

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40914239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:16:21  来源:igfitidea点击:

Spring Boot how to set spring.config.location in application.properties for yaml

springspring-bootpropertiesenvironment

提问by DANIEL K

I use spring boot 1.4.2 and now on try to set properties.

我使用 spring boot 1.4.2,现在尝试设置属性。

I have four cases about using .propertiesand .ymlto external(inside resources) and local file system (outside project folder).

我有四个关于将.properties.yml用于外部(内部资源)和本地文件系统(外部项目文件夹)的案例。

The .propertiesexternal (inside resource folder) works well both @Value(${property.name})and @PropertySourcelast one use value attribute can load from the file system like below.

的.properties外部(资源文件夹内),效果很好既@Value($ {} property.name)@PropertySource最后一个使用价值属性可以像下面的文件系统中加载。

@Component
@PropertySource(value="file:C:\properties\application-dev.properties")
@PropertySource(value="file:C:\properties\application-test.properties")
public class ExternalProperties {
   // ...
}

But .ymlisn't that works well under resource folder when the file name 'application.yml'

但是当文件名为“application.yml”时,.yml在资源文件夹下效果不佳

The .yml can be loaded by @ConfigurationProperties and need 'propdeps-plugin'

.yml 可以通过@ConfigurationProperties 加载并且需要“propdeps-plugin”

my .yml file

我的 .yml 文件

environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App

This code works well

这段代码运行良好

@Component
// external(inside resources)
@ConfigurationProperties(prefix="environments")
// outside project folder
@ConfigurationProperties(locations={"file:C:\properties\application.yml"}, prefix = "environments")
public class YamlProperties {

    private Map<String, String> dev = new HashMap<>();
    private Map<String, String> prod = new HashMap<>();

    public Map<String, String> getDev() {
        return this.dev;
    }

    public Map<String, String> getProd() {
        return this.prod;
    }
}

That code has problem of deprecatedlocation attribute (I read many articles but can't figure out clearly) so I need find API doc and find 'ConfigFileApplicationListener'from this section.

该代码存在不推荐使用的位置属性问题(我阅读了很多文章,但无法清楚地弄清楚)所以我需要找到 API 文档并从本节中找到“ConfigFileApplicationListener”

# SPRING CONFIG - using environment property only (ConfigFileApplicationListener)
spring.config.location= # Config file locations.
spring.config.name=application # Config file name.

So write above property on application.properties like this.

所以像这样在 application.properties 上写上面的属性。

spring.config.location=file:C:\properties\application.yml
spring.config.name=application

and reloading the .yml property (I didn't try excute jar. I use the war and test through Controller)

并重新加载 .yml 属性(我没有尝试执行 jar。我使用War并通过控制器进行测试)

@Component
@ConfigurationProperties(prefix="environments")
public class YamlProperties {

     private Map<String, String> dev = new HashMap<>();
     private Map<String, String> prod = new HashMap<>();

     public Map<String, String> getDev() {
         return this.dev;
     }

     public Map<String, String> getProd() {
         return this.prod;
     }
}

This code didn't load the .yml from local file system C: drive but when add application.yml file in the resource folder then works well.

此代码没有从本地文件系统 C: 驱动器加载 .yml,但是当在资源文件夹中添加 application.yml 文件时,效果很好。

So how to set the spring.config.locationfor load .yml

那么如何设置spring.config.location负载.yml

And I want to know about why the location attribute works yet although deprecated since 1.4 ver.

我想知道为什么 location 属性仍然有效,尽管自 1.4 版本以来已弃用。

and wondering how to use the ConfigFileApplicationListener I can't follow up the code it's hard to understand give some tip~!

并想知道如何使用 ConfigFileApplicationListener 我无法跟进代码很难理解给一些提示~!

EDIT:

I miss understood about this that when the war start again and make context again using the local file system properties. This is not the collectly linked it but remain for future step.

The tomcat restart than war deploy the new one so I have the file on local system it contain properties if I change this file's data that could be udated properties context when tomcat restart.

Why I keep try this work that I use public github account and protect connect db info something else to. I get this stuff and go on next issue like git-encrpt, spring-cloude, nginx, docker. Thank you for any help really helpful.

编辑:

我想念当War再次开始并使用本地文件系统属性再次创建上下文时的理解。这不是集体链接它,而是留待以后的步骤。

tomcat重启比war部署新的,所以我在本地系统上有文件,它包含属性,如果我改变这个文件的数据,当tomcat重启时可能是更新的属性上下文。

为什么我一直尝试使用公共 github 帐户并保护其他连接数据库信息的工作。我得到了这些东西,然后继续下一个问题,比如 git-encrpt、spring-cloude、nginx、docker。感谢您提供的任何帮助真的很有帮助。

回答by Gemini Keith

Since you are using spring boot 1.4.2, everything is simple. You just need specify additional argument for your application, such as java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.yml. A more common method to achieve this is defining additional option for JVM like java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar. Both of these two methods sould work, cause they work fine on my desktop.

由于您使用的是 spring boot 1.4.2,所以一切都很简单。您只需要为您的应用程序指定额外的参数,例如java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.yml. 实现此目的的更常见方法是为 JVM 定义附加选项,例如java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar. 这两种方法都可以工作,因为它们在我的桌面上工作正常。

回答by Mohith Maratt

You can use the spring.config.location property while invoking your Spring Boot application. Below is an example wherein you have 4 property files of custom name in custom locations.

您可以在调用 Spring Boot 应用程序时使用 spring.config.location 属性。下面是一个示例,其中您在自定义位置有 4 个自定义名称的属性文件。

java -Dspring.config.location=file:/gfs/config/post/post.properties,/gfs/config/post/post_pwd.properties,/myDbConfig/config/db-config.properties,/myserver/config/db-passwd.properties  -jar myapp-1.0-SNAPSHOT.jar

Within your code, you can access them by:

在您的代码中,您可以通过以下方式访问它们:

@Component
public class AppProperties {

    @Value("${post.user.name}")
    private String username;
}