问题描述
我在使用 spring 属性映射器时遇到了奇怪的问题.我有 yml 包含值列表.我想将其转换为为什么要构建应用程序的对象列表.所以,我使用了@ConfigurationProperties.有了这个,我就可以映射简单的类型.当我使用它来复杂类型(对象列表)时,它失败了.也不例外,但是当我调试时,值列表为零.请在下面找到 yml 和 java 文件.我试过 spring 2.0.0,2.0.1,2.0.2,2.0.3 没有成功.任何人都可以有办法修复它吗?
I am facing strange issue with spring property mapper. I have yml contains list of values. I want convert that to list of object why building application. So, i used @ConfigurationProperties. With this i am able to map simple types. when i use this to complex type(list of objects) it failed. No exception, but values list is zero when i debug. please find below yml and java files. I tried with spring 2.0.0,2.0.1,2.0.2,2.0.3 No success. Can any one have idea to fix it?
应用程序.yml
acme:
list:
- name: my name
description: my description
- name: another name
description: another description
AcmeProperties.java
AcmeProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@ConfigurationProperties("acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties {
private final List<MyPojo> list = new ArrayList<>();
public List<MyPojo> getList() {
return this.list;
}
static class MyPojo {
private String name;
private String description;
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
}
使用 setter 和 getter 方法:
With setter and getter methods:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties {
private List<MyPojo> list;
public List<MyPojo> getList() {
return list;
}
public void setList(List<MyPojo> list) {
this.list = list;
}
public static class MyPojo {
private String name;
private String 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;
}
}
}
这个类的用法:
@Autowired
public HomeController(AppProperties appProperties, AcmeProperties acmeProperties) {
this.appProperties = appProperties;
this.acmeProperties = acmeProperties;
}
推荐答案
问题是PropertySource
只支持属性文件,无法从yml
文件中读取值.你可以像这样更新它:
The problem is PropertySource
only support the properties file, you cannot read the value from yml
file. you can update it like:
@Component
@ConfigurationProperties("acme")
@PropertySource("classpath:/configuration/yml/test.properties")
class AcmeProperties {
configuration/yml/test.properties
acme.list[0].name=my name
acme.list[0].description=my description
acme.list[1].name=another name
acme.list[1].description=another description
代码应该可以工作.
这篇关于Spring @ConfigurationProperties 不映射对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!