[英]Reading a map from yaml in Java getting null
我在使用spring通過java閱讀yaml時遇到問題。 讓我先顯示代碼
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "countries")
public class UserLimitReader {
private HashMap<String, Integer> orders;
private HashMap<String, Integer> requests;
public HashMap<String, Integer> getOrders() {
return orders;
}
public void setOrderRedeemableLimit(HashMap<String, Integer> orders)
{
this.orders= orders;
}
public HashMap<String, Integer> getRequests() {
return requests;
}
public void setMonthlyRedeemableLimit(HashMap<String, Integer> requests) {
this.requests= requests;
}
}
我的yaml文件:
cassandra:
hosts: localhost:9142, 127.0.0.1:9142
keyspace: test
countries:
orders:
JPY: 1000
USD: 1000
requests:
JPY: 100000
USD: 100000
Spring上下文xml也具有以下內容:
<bean id="yamlProperties"
class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources">
<value>classpath:config/application.yaml</value>
</property>
</bean>
<context:property-placeholder
properties-ref="yamlProperties" />
現在,我的期望是,在我的春季測試應用程序運行期間(上下文xml來自我的測試資源,yaml也處於我的測試中),這些orders和requests值已設置,但它們為空。 另外,請注意,除了使用@Value($ {...})注入的值之外,yaml中還有其他值,注入它們絕對好!
我看了一下: Spring Boot-從application.yml注入映射
我幾乎做了同樣的事情,但是還沒有設置我的值。 請幫助。
我正在瀏覽google,找到了以下鏈接: http : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.html
它說,在這里,所有內容都被讀取為字符串而不是映射。 是否有其他類支持在此處讀取Yaml文件的方式: Spring Boot- 從application.yml注入映射
還是我對YamlPropertiesFactoryBean的理解錯誤?
compile 'org.springframework:spring-core:4.2.+'
compile 'org.springframework:spring-beans:4.2.+'
compile 'org.springframework:spring-context:4.2.+'
compile 'org.springframework.boot:spring-boot:1.3.1.RELEASE'
compile 'org.springframework.boot:spring-boot-configuration-processor:1.3.1.RELEASE'
這些是gradle中的依賴項。 您可能想知道為什么我要使用spring-core和spring-boot,從本質上講,我不希望使用spring-boot,但是如果沒有spring-boot,則不能添加@EnableConfigurationProperties和@ConfigurationProperties,老實說,我沒有知道如果沒有它們,我是否可以將Yaml內容讀入地圖。 因此,添加了這兩個依賴關系,但是如果有一種方法可以刪除它們,那么我很樂意刪除這兩個依賴關系。
親切的問候,帕萬
我在使用不同的泛型類型時遇到了相同的問題,我通過初始化map成員並刪除了setter方法來解決了這個問題,例如:
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "countries")
public class UserLimitReader{
private Map<String, Integer> orders = new HashMap<>();
private Map<String, Integer> requests = new HashMap<>();
public Map<String, Integer> getOrders() {
return orders;
}
...
}
請注意,我使用了Java 7 Diamond運算符,並將成員類型更改為Map而不是HashMap。
重要信息:在我的代碼中,我使用Spring的配置類而不是XML,並將EnableConfigurationProperties
移至配置類。 您的情況應該是這樣的:
@Configuration
@EnableConfigurationProperties(value = {UserLimitReader.class})
public class SpringConfiguration {
...
}
@ConfigurationProperties(prefix = "countries", locations: "classpath:config/application.yaml")
public class UserLimitReader {
...
}
不知道如何使用XML對其進行配置,但是正如我在評論中所寫的那樣,我仍然認為您應該確保Spring使用組件掃描或類似方法找到您的類。
@Value
正常工作,因為Spring使用上下文文件可以毫無問題地加載YAML文件,但這並不意味着UserLimitReader
文件由Spring加載和配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.