Spring Boot:使用@Value或@ConfigurationProperties从yaml读取列表

Mar*_*ged 14 configuration spring yaml snakeyaml spring-boot

我想从yaml文件(application.yml)中读取一个主机列表,该文件如下所示:

cors:
    hosts:
        allow: 
            - http://foo1/
            - http://foo2/
            - http://foo3/
Run Code Online (Sandbox Code Playgroud)

(例1)

我使用的类定义了这样的值:

@Value("${cors.hosts.allow}")   
List<String> allowedHosts;
Run Code Online (Sandbox Code Playgroud)

但是,当Spring抱怨这一点时,阅读失败了:

java.lang.IllegalArgumentException:无法在字符串值"$ {cors.hosts.allow}"中解析占位符'cors.hosts.allow'

当我像这样更改文件时,可以读取属性,但自然它不包含列表但只包含一个条目:

cors:
    hosts:
        allow: http://foo1, http://foo2, http://foo3
Run Code Online (Sandbox Code Playgroud)

(我知道我可以将这些值作为单行读取并将它们拆分","但我不想再找一个解决方法)

这也不起作用(虽然我认为根据snakeyamls文档这应该是有效的):

cors:
    hosts:
        allow: !!seq [ "http://foo1", "http://foo2" ] 
Run Code Online (Sandbox Code Playgroud)

(跳过!!seq并且只使用[/ ]也是一个失败)

我在这里阅读了一个建议,其中涉及使用@ConfigurationProperties并将示例转移到Java并将其与您在Example1中看到的yaml文件一起使用:

@Configuration
@EnableWebMvc
@ConfigurationProperties(prefix = "cors.hosts")
public class CorsConfiguration extends WebMvcConfigurerAdapter {
    @NotNull
    public List<String> allow;
...
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到了这个投诉:

org.springframework.validation.BindException:org.springframework.boot.bind.RelaxedDataBinder $ RelaxedBeanPropertyBindingResult:1 errors字段'allow'中对象'cors.hosts'中的字段错误:被拒绝的值[null]; 代码[NotNull.cors.hosts.allow,NotNull.allow,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [cors.hosts.allow,allow]; 争论者[]; 默认消息[allow]];

我搜索了其他方法让我的CORS主机可配置并找到了这个Spring Boot问题,但由于还没有完成,我不能用它作为解决方案.所有这些都是通过Spring Boot 1.3 RC1完成的

Jea*_*ean 7

这很容易,答案是在这个文档,并在这一个

因此,您有一个这样的Yaml:

cors:
    hosts:
        allow: 
            - http://foo1/
            - http://foo2/
            - http://foo3/
Run Code Online (Sandbox Code Playgroud)

然后您首先绑定数据

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
}
Run Code Online (Sandbox Code Playgroud)

然后在另一个组件中

@Autowired
private AllowedHosts allowedHosts;
Run Code Online (Sandbox Code Playgroud)

您完成了!


Ahm*_*gaç 6

在application.yml中使用逗号分隔的值

corsHostsAllow: http://foo1/, http://foo2/, http://foo3/
Run Code Online (Sandbox Code Playgroud)

用于访问的Java代码

@Value("${corsHostsAllow}")    
String[] corsHostsAllow
Run Code Online (Sandbox Code Playgroud)

我尝试并成功;)


小智 3

我已经能够从属性中读取列表,如下所示-

特性-

cors.hosts.allow[0]=host-0
cors.hosts.allow[1]=host-1
Run Code Online (Sandbox Code Playgroud)

阅读财产-

@ConfigurationProperties("cors.hosts")
public class ReadProperties {
    private List<String> allow;

    public List<String> getAllow() {
       return allow;
    }
    public void setAllow(List<String> allow) {
        this.allow = allow;
    }
}
Run Code Online (Sandbox Code Playgroud)