[英]Spring @ConfigurationProperties not mapping list of objects
I am facing strange issue with spring property mapper. 我面临着Spring属性映射器的奇怪问题。 I have yml contains list of values.
我有yml包含值列表。 I want convert that to list of object why building application.
我想将其转换为对象列表,以构建应用程序。 So, i used @ConfigurationProperties.
因此,我使用了@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.
请在下面找到yml和java文件。 I tried with spring 2.0.0,2.0.1,2.0.2,2.0.3 No success.
我尝试使用spring 2.0.0,2.0.1,2.0.2,2.0.3没有成功。 Can any one have idea to fix it?
有人可以解决这个问题吗?
application.yml application.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;
}
}
}
With setter and getter methods: 使用setter和getter方法:
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;
}
}
}
Usage of this class: 该类的用法:
@Autowired
public HomeController(AppProperties appProperties, AcmeProperties acmeProperties) {
this.appProperties = appProperties;
this.acmeProperties = acmeProperties;
}
The problem is PropertySource
only support the properties file, you cannot read the value from yml
file. 问题是
PropertySource
仅支持属性文件,无法从yml
文件读取值。 you can update it like: 您可以像这样更新它:
@Component
@ConfigurationProperties("acme")
@PropertySource("classpath:/configuration/yml/test.properties")
class AcmeProperties {
configuration/yml/test.properties 配置/ 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
and the code should works. 和代码应该工作。
According to the Spring Boot documentation (emphasis is mine) : 根据Spring Boot文档 (重点是我的):
24.6.4 YAML Shortcomings
24.6.4 YAML的缺点
YAML files cannot be loaded by using the @PropertySource annotation .
无法使用@PropertySource批注加载YAML文件 。 So, in the case that you need to load values that way, you need to use a properties file.
因此,在需要以这种方式加载值的情况下,需要使用属性文件。
But you provided a yml file to @PropertySource
: 但是您向
@PropertySource
提供了yml文件:
@Component
@ConfigurationProperties(prefix = "acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties { ...}
So you have two possibilities : 因此,您有两种可能性:
@PropertySource
@PropertySource
Try with @ConfigurationProperties(prefix = "acme")
尝试使用
@ConfigurationProperties(prefix = "acme")
Based on: https://aykutakin.wordpress.com/2016/02/26/injecting-list-with-spring-from-yaml/ 基于: https : //aykutakin.wordpress.com/2016/02/26/injecting-list-with-spring-from-yaml/
I think problem is that resource is not found as specified at @PropertySource("classpath:configuration/yml/application.yml"). 我认为问题是找不到在@PropertySource(“ classpath:configuration / yml / application.yml”)中指定的资源。 -made the same mistake when tried to debug your code..- Could you please put a break point in org.springframework.context.annotation.ConfigurationClassParser#processPropertySource with watch check path returned from this.resourceLoader.getResource(resolvedLocation).getFile().
-尝试调试代码时犯同样的错误。.-您能否在org.springframework.context.annotation.ConfigurationClassParser#processPropertySource中放置一个断点,并从this.resourceLoader.getResource(resolvedLocation).getFile( )。
In my case i have: C:\\Users\\user12\\Desktop\\hibernate\\out\\production\\classes\\configuration\\application.yml 就我而言,我有:C:\\ Users \\ user12 \\ Desktop \\ hibernate \\ out \\ production \\ classes \\ configuration \\ application.yml
there there is not application.yml... 那里没有application.yml ...
I think you do not see exception because spring has if (ignoreResourceNotFound) {..} else {throw ex} in this method 我认为您看不到异常,因为spring在此方法中具有if(ignoreResourceNotFound){..} else {throw ex}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.