Reading a map from yaml in Java getting null

I have a problem reading yaml via java using spring. 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:

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

The xml spring context also has the following:

<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 wait that I had (had) while my spring -test application was running (contextual xml from my test resources, yaml is also in my test), these order values ​​and requests are set, but they are zero. Also, note that there are other values ​​in my barley, except that I inject using @Value ($ {...}), they are entered exactly!

I looked at this: Spring Download the input map from the .yml application

I did pretty much the same thing, but still my values ​​are not set. Kindly help.

Google, : http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.html

, , , . , Yaml , : Spring 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 . , , , .

,

+4
1

, , 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 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.

+4

Source: https://habr.com/ru/post/1623585/


All Articles