我在使用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" />
现在,我的期望是,在我的 Spring 测试应用程序运行期间(上下文xml来自我的测试资源,yaml也处于我的测试中),这些orders和requests值都已设置,但它们为空。另外,请注意,除了使用@Value($ {...})注入(inject)的值之外,yaml中还有其他值,注入(inject)它们绝对好!
我看着这个:Spring Boot - inject map from application.yml
我几乎做了同样的事情,但是还没有设置我的值。请帮助。
我正在浏览google,发现此链接:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.html
它说,在这里,所有内容都被读取为字符串,而不是映射。是否有其他类支持在此处读取Yaml文件的方式:Spring Boot - inject map from 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 。因此,添加了这两个依赖关系,但是如果有一种方法可以删除它们,那么我很乐意删除这两个依赖关系。
温暖的问候,
帕万
最佳答案
我在使用不同的泛型类型时遇到了同样的问题,我通过初始化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加载和配置的。关于java - 在Java中从Yaml读取 map 获取空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34697611/