简体   繁体   English

Spring Boot Yaml配置:类型化属性列表

[英]Spring Boot Yaml configuration: list of typed properties

I'm following the 24.8.3 Merging Complex Types section of Spring Boot's 24. Externalized Configuration documentation. 我正在遵循Spring Boot的24. Externalized Configuration文档的24.8.3合并复杂类型部分。 I have this config.yaml file: 我有这个config.yaml文件:

acme:
  list:
    - name: my name
      description: my description
    - name: another name
      description: another description

The Properties file looks like this: 属性文件如下所示:

@ConfigurationProperties("acme")
@YamlPropertySource(value = { "classpath:/config.yaml" })
public class AcmeProperties {

    private final List<MyPojo> list = new ArrayList<>();

    public List<MyPojo> getList() {
        return this.list;
    }
}

The MyPojo class: MyPojo类:

public class MyPojo {
    private String name;
    private String description;

    public MyPojo(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

The test, which fails, looks like this: 测试失败,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AcmeProperties.class })
public class AcmePropertiesTest {

    @Autowired
    private AcmeProperties properties;

    @Test
    public void getOpScoringClusters() {
        Assert.assertEquals(2, properties.getList().size()); // FAIL!
    }
}

Spring Boot version 1.5.6. Spring Boot版本1.5.6。

Basically I want to have a list of typed properties. 基本上,我想要一个类型化属性的列表。 What am I doing wrong? 我究竟做错了什么?

Several comments have highlighted multiple issues with the code presented. 一些注释突出了所提供代码的多个问题。

Firstly, the fields inside a configuration properties can't be final as spring uses the setter to set the value. 首先,由于spring使用setter来设置值,所以配置属性内的字段不能是final。 Secondly, @YamlPropertySource is not something provided by spring so won't do anything in this context. 其次, @YamlPropertySource不是spring提供的,因此在这种情况下不会做任何事情。 Thirdly, even if you did use the spring PropertySource annotation, unfortunately you can't use it with yaml files. 第三,不幸的是,即使您确实使用了Spring PropertySource注解,也无法将其与yaml文件一起使用。

YAML files cannot be loaded by using the @PropertySource annotation. 无法使用@PropertySource批注加载YAML文件。

I've created a sample project that uses the code you presented and has been modified so that it passes the unit test. 我创建了一个示例项目,该项目使用您提供的代码并进行了修改,以使其通过单元测试。 It's using spring boot 2.x instead of 1.x but the only significant difference should be the annotations used in the test class. 它使用的是Spring Boot 2.x而不是1.x,但是唯一的区别应该是测试类中使用的注释。

https://github.com/michaelmcfadyen/spring-boot-config-props-demo https://github.com/michaelmcfadyen/spring-boot-config-props-demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM