Spring Boot:使用@Value或@ConfigurationProperties从yaml读取列表 [英] Spring Boot: read list from yaml using @Value or @ConfigurationProperties
问题描述
我想从yaml文件(application.yml)中读取主机列表,文件如下所示:
I want to read a list of hosts from a yaml file (application.yml), the file looks like this:
cors:
hosts:
allow:
- http://foo1/
- http://foo2/
- http://foo3/
(示例1)
值如下:
@Value("${cors.hosts.allow}")
List<String> allowedHosts;
但是阅读失败,因为Spring抱怨说:
But reading fails as Spring complains about this:
java.lang.IllegalArgumentException:无法在字符串值$ {cors.hosts.allow}中解析占位符
'cors.hosts.allow'
java.lang.IllegalArgumentException: Could not resolve placeholder 'cors.hosts.allow' in string value "${cors.hosts.allow}"
当我这样更改文件时,属性可以被读取,但自然不包含列表,只有一个条目:
When I change the file like this the property can be read but naturally it does not contain the list but only one entry:
cors:
hosts:
allow: http://foo1, http://foo2, http://foo3
(我知道我可以读取这些值为一行,并用 ,
,但我不想要一个解决方法)
(I know that I could read the values as a single line and split them by ","
but I do not want to go for a workaround yet)
这不工作(虽然我认为这应该是有效的到 snakeyamls文档):
This does not work either (although I think this should be valid according to snakeyamls docs):
cors:
hosts:
allow: !!seq [ "http://foo1", "http://foo2" ]
(跳过 !! seq
,只需使用
(Skipping the !!seq
and just using the [
/ ]
is a failure too)
I read the suggestion here which involves using @ConfigurationProperties
and transferred the example to Java and used it with the yaml file you see in Example1:
@Configuration
@EnableWebMvc
@ConfigurationProperties(prefix = "cors.hosts")
public class CorsConfiguration extends WebMvcConfigurerAdapter {
@NotNull
public List<String> allow;
...
当我执行此操作时,我收到此投诉:
When I run this I get this complaint:
org.springframework.validation.BindException:
org.springframework.boot.bind.RelaxedDataBinder $ RelaxedBeanPropertyBindingResult:
1错误对象'cors.hosts'在字段'allow':rejected
value [null]; codes [NotNull.cors.hosts.allow,NotNull.allow,NotNull];
arguments
[org.springframework.context.support.DefaultMessageSourceResolvable:
codes [cors.hosts.allow,allow]; argumen ts [];默认消息
[允许]];
org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors Field error in object 'cors.hosts' on field 'allow': rejected value [null]; codes [NotNull.cors.hosts.allow,NotNull.allow,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [cors.hosts.allow,allow]; argumen ts []; default message [allow]];
我搜索其他方法让我的CORS主机配置, href =https://github.com/spring-projects/spring-boot/issues/3052> Spring启动问题,但因为这还没有完成,我不能使用它作为一个解决方案。
所有这一切都是使用Spring Boot 1.3 RC1
I searched for other means to have my CORS hosts configurable and found this Spring Boot issue but as this is not yet finished I can't use it as a solution. All of this is done with Spring Boot 1.3 RC1
推荐答案
It's easy, the answer is in this doc and also in this one
所以,你有一个这样的yaml:
So, you have a yaml like this:
cors:
hosts:
allow:
- http://foo1/
- http://foo2/
- http://foo3/
/ p>
Then you first bind the data
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix="cors.hosts")
public class AllowedHosts {
private List<String> HostNames; //You can also bind more type-safe objects
}
只是做
@Autowired
private AllowedHosts allowedHosts;
完成了!
这篇关于Spring Boot:使用@Value或@ConfigurationProperties从yaml读取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!