Christian Rockrohr
Christian Rockrohr

Reputation: 1045

How to fill map, configured with spring, from properties file

I have built a small app that reads an Excel file and creates all the necessary SQL statements for me. Excel file is manipulated by customers from time to time.

The first line of the Excel file includes a title I need to translate into integers when reading the lines. For example, the title "english" needs to be translated into "30" before I generate the SQL statements. (Just some internal definitions). (You could compare it with DNS - Human readable domain name into IP-Address mapping) Currently I do the mapping manually, but I want to get rid of this step with a tiny Spring configuration. Because the headlines are not fixed, it is necessary to read the headline information from a properties file.

I found something like this:

<bean class="java.util.HashMap" id="exampleMapping">
    <constructor-arg index="0">
        <map>
            <entry key="theKey" value="theValue"/>
            <entry key="otherKey" value="otherValue"/>
        </map>
    </constructor-arg>
</bean>

Which seems to work quite well. But the Spring configuration is compiled into a jar. So my first choice (if possible) would be to externalize the key value pairs into a properties file.

Upvotes: 0

Views: 7730

Answers (1)

ach
ach

Reputation: 6234

You're in luck, because the Properties class implements Map! Just define the properties bean like so:

<util:properties id="myProps" location="classpath:myProps.properties" />

(Don't forget to import the Spring util namespace)

Upvotes: 8

Related Questions