[英]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.