Spring Boot:从 YAML 文件加载 @Value

Spring Boot: Load @Value from YAML file(Spring Boot:从 YAML 文件加载 @Value)
本文介绍了Spring Boot:从 YAML 文件加载 @Value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要从 .yml 文件中加载一个属性,该文件包含应用程序可以从中读取文件的文件夹的路径.

I need to load a property from a .yml file, which contains the path to a folder where the application can read files from.

我正在使用以下代码注入属性:

I'm using the following code to inject the property:

@Value("${files.upload.baseDir}")
private String pathToFileFolder;

用于开发的 .yml 文件位于 src/main/resources/config/application.yml 下,我在生产中使用以下命令运行应用程序,以覆盖开发设置:

The .yml file for development is located under src/main/resources/config/application.yml, im running the application with the following command in production, to override the development settings:

java -jar app.jar --spring.config.location=/path/to/application-production.yml

Spring Boot 文档说:

The Spring Boot documentation says:

SpringApplication 将从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  1. 当前目录的/config 子目录.

  1. A /config subdirectory of the current directory.

当前目录

类路径/config 包

A classpath /config package

类路径根

还有:

您还可以使用 YAML ('.yml') 文件来替代.properties".

You can also use YAML ('.yml') files as an alternative to '.properties'.

.yml 文件包含:

{...}
files:
      upload:
        baseDir: /Users/Thomas/Code/IdeaProjects/project1/files
{...}

我的 Application 类被注释为:

@SpringBootApplication
@EnableCaching

当我运行应用程序时,我得到一个异常:

When I run the application, i get an exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'files.upload.baseDir' in string value "${files.upload.baseDir}"

是否必须使用 YamlPropertySourceLoader 类或添加特殊注解才能在 Spring Boot 中启用对 .yml 的支持?

Do I have to use the YamlPropertySourceLoader class or add a special annotation to enable the support for .yml in Spring Boot?

.yml 文件包含一些其他属性,这些属性可以由 Spring Boot 成功加载,例如 dataSource.XXXhibernate.XXX.

The .yml file contains some other properties, which get successfully loaded by Spring Boot like dataSource.XXXor hibernate.XXX.

推荐答案

M.Deinum 是对的,我提供的设置正在运行 - yml 文件缩进错误,因此无法找到该属性.

M. Deinum is right, the setup i've provided is working - the yml file was indented wrong, so the property couldn't be found.

这篇关于Spring Boot:从 YAML 文件加载 @Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)