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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:46:18  来源:igfitidea点击:

Spring Boot: Load @Value from YAML file

javaspringspring-bootyaml

提问by Thomas Schmidt

I need to load a property from a .ymlfile, 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 .ymlfile 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:

  1. A /config subdirectory of the current directory.

  2. The current directory

  3. A classpath /config package

  4. The classpath root

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

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

  2. 当前目录

  3. 一个类路径 /config 包

  4. 类路径根

As well as:

也:

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

您还可以使用 YAML ('.yml') 文件作为 '.properties' 的替代文件。

The .ymlfile contains:

.yml文件包含:

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

And my Applicationclass 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 YamlPropertySourceLoaderclass or add a special annotation to enable the support for .ymlin Spring Boot?

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

Edit: The .ymlfile contains some other properties, which get successfully loaded by Spring Boot like dataSource.XXXor hibernate.XXX.

编辑:该.yml文件包含一些其他属性,这些属性可以通过 Spring Boot 成功加载,例如dataSource.XXXhibernate.XXX

采纳答案by Thomas Schmidt

M. Deinum is right, the setup i've provided is working - the ymlfile 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.

也许这对其他人有帮助。