繁体   English   中英

Spring Boot和@ConfigurationProperties

[英]Spring Boot and @ConfigurationProperties

我希望(如果可能的话)使用@ConfigurationProperties来创建动态大小的POJO列表。 请告知是否可以。 我的想法是如下所示(省略了args构造函数/ getter / setter):

属性文件:

my.item[0].prop1=a
my.item[0].prop2=b

my.item[1].prop1=a
my.item[1].prop2=b

应该填充的bean:

@Component
@ConfigurationProperties(prefix = "my")
public class ItemsConfig {

    private List<Item> items;

    public static class Item {
        private String prop1;
        private String prop2;
    }
}

不幸的是,当@Autowire ItemsConfig ,列表始终为null

使用@ConfigurationProeprties可以实现类似的功能吗?

我找到了一个解决方法,使用BeanFactoryPostProcessor迭代属性并手动创建所有内容,使其可怕的代码:(

请指教

PS:我在@Configuration上使用@EnableConfigurationProperties

注意:一旦解决了,人们可能会发现在@EnableConfigurationProperties创建带有@ConfigurationProperties的组件之前必须找到并处理@EnableConfigurationProperties注释是有用的。 否则将不会填充bean。

属性条目有一个小问题,应该如下:

my.items[0].prop1=a
my.items[0].prop2=b

my.items[1].prop1=a
my.items[1].prop2=b

注意itemsitem ,以匹配设置者名称

暂无
暂无

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

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