Java Spring Boot:从 YAML 文件加载 @Value
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34556677/
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
Spring Boot: Load @Value from YAML file
提问by Thomas Schmidt
I need to load a property from a .yml
file, which contains the path to a folder where the application can read files from.
我需要从.yml
文件加载一个属性,该文件包含应用程序可以从中读取文件的文件夹的路径。
I'm using the following code to inject the property:
我正在使用以下代码注入属性:
@Value("${files.upload.baseDir}")
private String pathToFileFolder;
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:
在.yml
发展文件位于下src/main/resources/config/application.yml
,即时通讯运行在生产下列命令的应用程序,覆盖开发设置:
java -jar app.jar --spring.config.location=/path/to/application-production.yml
The Spring Boot documentation says:
Spring Boot 文档说:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
SpringApplication 将从以下位置的 application.properties 文件加载属性并将它们添加到 Spring Environment:
当前目录的 /config 子目录。
当前目录
一个类路径 /config 包
类路径根
As well as:
也:
You can also use YAML ('.yml') files as an alternative to '.properties'.
您还可以使用 YAML ('.yml') 文件作为 '.properties' 的替代文件。
The .yml
file contains:
该.yml
文件包含:
{...}
files:
upload:
baseDir: /Users/Thomas/Code/IdeaProjects/project1/files
{...}
And my Application
class is annotated with:
我的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}"
Do I have to use the YamlPropertySourceLoader
class or add a special annotation to enable the support for .yml
in Spring Boot?
我是否必须使用YamlPropertySourceLoader
该类或添加特殊注释才能.yml
在 Spring Boot 中启用对 的支持?
Edit:
The .yml
file contains some other properties, which get successfully loaded by Spring Boot like dataSource.XXX
or hibernate.XXX
.
编辑:该.yml
文件包含一些其他属性,这些属性可以通过 Spring Boot 成功加载,例如dataSource.XXX
或hibernate.XXX
。
采纳答案by Thomas Schmidt
M. Deinum is right, the setup i've provided is working - the yml
file was indented wrong, so the property couldn't be found.
M. Deinum 是对的,我提供的设置正在运行 -yml
文件缩进错误,因此无法找到该属性。
回答by SatyaRajC
For me a duplicate key in the property file caused this...
对我来说,属性文件中的重复键导致了这个......
I used same key unknowingly in large yml file.
我在大 yml 文件中不知不觉地使用了相同的密钥。
key:
key1: value
key2: value
key:
key3: value
回答by Nick Kras Borges
For example: application.yml
例如:application.yml
key:
name: description here
Your Class:
你的班:
@Value("${key.name}")
private String abc;
回答by Kjeld
I found the above wasn't working for me, because I tried to access the variable in a constructor. But at construction, the value is not injected yet. Eventually I got it to work using this workaround: https://mrhaki.blogspot.com/2015/04/spring-sweets-using-value-for.html
我发现上面的方法对我不起作用,因为我试图在构造函数中访问变量。但是在构建时,尚未注入该值。最终我使用这个解决方法让它工作:https: //mrhaki.blogspot.com/2015/04/spring-sweets-using-value-for.html
Maybe this is helpful to others.
也许这对其他人有帮助。