简体   繁体   中英

accessing map property from .properties file in application context (Spring)

In my .properties file I have something like this:

map = key1=value1, key2=value2

How do I access the key-value pairs in my applicationContext.xml using placeholders? I know that if the property were just a string it would simply be:

<bean id="string_prop" class="java.lang.String">
    <constructor-arg value="${string.prop}"/>
</bean>

I've also seen this:

<util:map id="map_prop" key-type="java.lang.String" value-type="java.lang.String">
    <entry key="key" value="value"></entry>
</util:map>

But I'm not sure how to access the key-value pairs from the .properties file.

If you want to access the map in java code means use like below,

In applicationContext.xml

<bean id="mapName" class="java.util.HashMap">
        <constructor-arg ref="property" /> 
</bean>

<util:properties id="property" location="properName.properties"/>

In java:

 @Autowired
    protected HashMap<String, String> mapName;

If you want from applicationContext itself means use like below,

In applicationContext.xml

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="propertyName.properties"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

<bean id="string_prop" class="java.lang.String">
    <constructor-arg value="${string.prop}"/>
</bean>

In properties file:

string.prop=some name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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