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

标签 spring spring-boot properties environment

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

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

.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 {
   // ...
}

但是 .yml 当文件名“application.yml”时,在资源文件夹下不是很好吗

.yml 可以由 @ConfigurationProperties 加载并且需要'propdeps-plugin'

我的 .yml 文件
environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App

此代码运行良好
@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;
    }
}

该代码有 的问题已弃用 location 属性(我读了很多文章但无法弄清楚)所以我需要找到 API doc 并找到 'ConfigFileApplicationListener' 从此section .
# SPRING CONFIG - using environment property only (ConfigFileApplicationListener)
spring.config.location= # Config file locations.
spring.config.name=application # Config file name.

所以像这样在 application.properties 上写上面的属性。
spring.config.location=file:C:\\properties\\application.yml
spring.config.name=application

并重新加载 .yml 属性(我没有尝试执行 jar。我使用 war 并通过 Controller 进行测试)
@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 文件时效果很好。

那么如何设置 spring.config.location 用于加载 .yml

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

并且想知道如何使用 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.

最佳答案

由于您使用的是 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。 .这两种方法都可以工作,因为它们在我的桌面上工作正常。

关于Spring Boot 如何在 application.properties 中为 yaml 设置 spring.config.location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40914239/

相关文章:

javax.servlet.ServletException : javax. servlet.jsp.JspTagException:在语言环境 'textv' 的代码 'pl_PL' 下找不到消息

spring-boot - Vaadin 流程应用程序无法与 Spring Boot 2.6.0 配合使用

spring - Camel 化 Spring 启动应用程序

java - 添加Spring Security导致列和表丢失

java - 如何将系统属性传递给从 HTML 启动的 Java 小程序

Java : How to handle file size exceed exception of tomcat

java - 从 HandlerInterceptor 抛出 HTTP 状态代码异常

github - 从 github 存储库加载时 Gradle 独立插件未知属性

cocoa - 添加复合属性以绑定(bind)到现有类的最佳方法是什么

hibernate - 命名查询中的 hibernate 设置参数