[英]Spring @ConfigurationProperties not mapping list of objects
我面臨着Spring屬性映射器的奇怪問題。 我有yml包含值列表。 我想將其轉換為對象列表,以構建應用程序。 因此,我使用了@ConfigurationProperties。 有了這個我就可以映射簡單的類型。 當我使用它來復雜類型(對象列表)時,它失敗了。 也不例外,但是當我調試時值列表為零。 請在下面找到yml和java文件。 我嘗試使用spring 2.0.0,2.0.1,2.0.2,2.0.3沒有成功。 有人可以解決這個問題嗎?
application.yml
acme:
list:
- name: my name
description: my description
- name: another name
description: another description
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方法:
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
文件讀取值。 您可以像這樣更新它:
@Component
@ConfigurationProperties("acme")
@PropertySource("classpath:/configuration/yml/test.properties")
class AcmeProperties {
配置/ 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 Boot文檔 (重點是我的):
24.6.4 YAML的缺點
無法使用@PropertySource批注加載YAML文件 。 因此,在需要以這種方式加載值的情況下,需要使用屬性文件。
但是您向@PropertySource
提供了yml文件:
@Component
@ConfigurationProperties(prefix = "acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties { ...}
因此,您有兩種可能性:
@PropertySource
嘗試使用@ConfigurationProperties(prefix = "acme")
基於: https : //aykutakin.wordpress.com/2016/02/26/injecting-list-with-spring-from-yaml/
我認為問題是找不到在@PropertySource(“ classpath:configuration / yml / application.yml”)中指定的資源。 -嘗試調試代碼時犯同樣的錯誤。.-您能否在org.springframework.context.annotation.ConfigurationClassParser#processPropertySource中放置一個斷點,並從this.resourceLoader.getResource(resolvedLocation).getFile( )。
就我而言,我有:C:\\ Users \\ user12 \\ Desktop \\ hibernate \\ out \\ production \\ classes \\ configuration \\ application.yml
那里沒有application.yml ...
我認為您看不到異常,因為spring在此方法中具有if(ignoreResourceNotFound){..} else {throw ex}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.