简体   繁体   English

在Java中从Yaml读取地图获取空值

[英]Reading a map from yaml in Java getting null

I am getting a problem in reading my yaml through java, using spring. 我在使用spring通过java阅读yaml时遇到问题。 Let me show the code first 让我先显示代码

@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;
}
}

My yaml file: 我的yaml文件:

cassandra:
    hosts: localhost:9142, 127.0.0.1:9142
    keyspace: test
countries:
    orders:
      JPY: 1000
      USD: 1000
    requests:
      JPY: 100000
      USD: 100000

The spring context xml also has this: 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" />

Now, the expectation that I have(had) is that, during the runtime of my spring-test application(the context xml is from my test resources, the yaml is also in my test), these values of orders and requests are set, but they are null. 现在,我的期望是,在我的春季测试应用程序运行期间(上下文xml来自我的测试资源,yaml也处于我的测试中),这些orders和requests值已设置,但它们为空。 Also, note, there are other values in my yaml besides this that I am injecting using @Value(${...}), they are injected absolutely fine! 另外,请注意,除了使用@Value($ {...})注入的值之外,yaml中还有其他值,注入它们绝对好!

I looked at this: Spring Boot - inject map from application.yml 我看了一下: Spring Boot-从application.yml注入映射

I have done virtually the same, but yet, my values are not set. 我几乎做了同样的事情,但是还没有设置我的值。 Kindly help. 请帮助。

I was going through google, found this link: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.html 我正在浏览google,找到了以下链接: http : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.html

In this, it says, everything is read as a string and not a map. 它说,在这里,所有内容都被读取为字符串而不是映射。 Is there any other class that supports reading of Yaml file the way they did it here: Spring Boot - inject map from application.yml 是否有其他类支持在此处读取Yaml文件的方式: Spring Boot- 从application.yml注入映射

Or is my understanding of YamlPropertiesFactoryBean wrong? 还是我对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'

These are the dependencies in gradle. 这些是gradle中的依赖项。 You might be wondering why I have spring-core and spring-boot, essentially, I don't want spring-boot, but without spring-boot, I don't get to add @EnableConfigurationProperties and @ConfigurationProperties, I honestly don't know if I can read the yaml content into a map without them. 您可能想知道为什么我要使用spring-core和spring-boot,从本质上讲,我不希望使用spring-boot,但是如果没有spring-boot,则不能添加@EnableConfigurationProperties和@ConfigurationProperties,老实说,我没有知道如果没有它们,我是否可以将Yaml内容读入地图。 Hence those two dependencies are added, but if there's a way to remove them, I would be more than happy to remove those two dependencies. 因此,添加了这两个依赖关系,但是如果有一种方法可以删除它们,那么我很乐意删除这两个依赖关系。

Warm regards, Pavan 亲切的问候,帕万

I had the same problem with different generic types and I solved it by initializing the map member and remove the setter method, eg: 我在使用不同的泛型类型时遇到了相同的问题,我通过初始化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;
    }
    ...
}

Please note I used java 7 diamond operator and change the member type to Map instead of HashMap. 请注意,我使用了Java 7 Diamond运算符,并将成员类型更改为Map而不是HashMap。

IMPORTANT: In my code, I use Spring's configuration class instead of XML and moved the EnableConfigurationProperties to the configuration class. 重要信息:在我的代码中,我使用Spring的配置类而不是XML,并将EnableConfigurationProperties移至配置类。 In your case it should be something like: 您的情况应该是这样的:

@Configuration
@EnableConfigurationProperties(value = {UserLimitReader.class})
public class SpringConfiguration {
    ...
}

@ConfigurationProperties(prefix = "countries", locations: "classpath:config/application.yaml")
public class UserLimitReader {
    ...
}

Don't know how you configure it using XML, however as I wrote in my comments I still think you should make sure Spring's finds your class using component scan or something alike. 不知道如何使用XML对其进行配置,但是正如我在评论中所写的那样,我仍然认为您应该确保Spring使用组件扫描或类似方法找到您的类。

@Value work as Spring loads the YAML file without any problem using your context file, however it does not mean UserLimitReader file is loaded and configured by Spring. @Value正常工作,因为Spring使用上下文文件可以毫无问题地加载YAML文件,但这并不意味着UserLimitReader文件由Spring加载和配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM