首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Boot如何在application.properties中为yaml设置spring.config.location

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

Stack Overflow用户
提问于 2016-12-01 23:17:53
回答 2查看 33.3K关注 0票数 6

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

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

@Value(${property.name})@PropertySource最后一个使用值属性可以从文件系统加载,.properties外部(在资源文件夹内)工作良好,如下所示。

代码语言:javascript
复制
@Component
@PropertySource(value="file:C:\\properties\\application-dev.properties")
@PropertySource(value="file:C:\\properties\\application-test.properties")
public class ExternalProperties {
   // ...
}

但是当文件名为'application.yml‘时,.yml在资源文件夹下不能很好地工作。

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

我的.yml文件

代码语言:javascript
复制
environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App

这段代码运行良好

代码语言:javascript
复制
@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;
    }
}

该代码存在已弃用的位置属性的问题(我读了很多文章,但都搞不清楚),所以我需要从这个section中找到API文档和'ConfigFileApplicationListener'

代码语言:javascript
复制
# SPRING CONFIG - using environment property only (ConfigFileApplicationListener)
spring.config.location= # Config file locations.
spring.config.name=application # Config file name.

因此,在application.properties上编写上述属性如下所示。

代码语言:javascript
复制
spring.config.location=file:C:\\properties\\application.yml
spring.config.name=application

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

代码语言:javascript
复制
@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;
     }
}

这段代码没有从本地文件系统C:.yml加载驱动器,但是当在资源文件夹中添加application.yml文件时,它工作得很好。

那么如何为load .yml设置spring.config.location呢?

我想知道为什么location属性仍然有效,尽管从1.4版本开始就不推荐使用它了。

想知道如何使用ConfigFileApplicationListener,我跟不上代码,很难理解,给点小贴士~!

编辑:

当war再次启动并使用本地文件系统属性再次创建上下文时,我错过了对此的理解。这不是集合链接,而是为将来的步骤保留。

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

为什么我一直尝试这项工作,我使用公共github帐户和保护连接数据库信息到其他东西。我得到了这些东西,然后继续下一期,比如git-encrpt,spring-cloude,nginx,docker。谢谢你的帮助,真的很有用。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40914239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档