Results 1 to 9 of 9

Thread: Define a map in a .properties file using PropertyPlaceholderConfigurer?

  1. #1
    Join Date
    Mar 2007
    Posts
    2

    Question Define a map in a .properties file using PropertyPlaceholderConfigurer?

    Is it possible to define a map into a .properties file?

    Currently, I have a map in my applicationContext.xml, but since more keys and values are likely to be added (or removed) in the future I would like to move them into a project.properties file.

    This is what I have:
    Code:
    <bean name="myBean" class="myPackage.myClass">
       <property name="myMap">
          <map>
             <entry>
                <key>
                   <value>key1</value>
                </key>
                   <value>value1</value>
             </entry>
             <entry>
                <key>
                   <value>key2</value>
                </key>
                <value>value2</value>
             </entry>
             <!-- more key / value pairs -->
          </map>
       </property>
    </bean>
    This is what I would like to have:
    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location">
          <value>project.properties</value>
       </property>
    </bean>
    <bean name="myBean" class="myPackage.myClass">
       <property name="myMap">
          <value>${myMapInProjectProperties}</value>
       </property>
    </bean>

    1. Is this possible?
    2. How do define the key / value pairs in project.properties?
    3. Is the syntax correct in the last bean that I stated?



    BR, Mattias

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Couldn't you just do something like this?
    Code:
    	<bean id="myBean" class="myPackage.myClass">
    		<property name="myMap">
    			<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    				<property name="location" value="project.properties"/>
    			</bean>
    		</property>
    	</bean>

  3. #3
    Join Date
    Mar 2007
    Posts
    2

    Default

    Well, that seems to work if I have only the map in the project.properties file.

    However, I have some other parameters to be set in that file as well:

    From project.properties:
    Code:
    myMapInProjectProperties = key1 = value1, key2 = value2
    
    myOtherParameter = someOtherValue
    From applicationContext.xml:
    Code:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location" value="project.properties"/>
    </bean>
    
    <bean id="myBean" class="myPackage.myClass">
       <property name="myMap">
          <value>${myMapInProjectProperties}</value>
       </property>
    </bean>
    
    <bean id="myOtherBean" class="myPackage.myOtherClass">
       <property name="myParam">
          <value>${myOtherParameter}</value>
       </property>
    </bean>
    Obviously, this does not work. Please advice.

    /Mattias

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    The simplest approach would be to have multiple properties-files.

    If you do not like that, I think you have to create an own BeanFactoryPostProcessor to handle this case. Of course you have to consider how to identify the parameters belonging together.
    That could be done, for example, by specifying a common prefix:
    mymap.key1=value1
    mymap.key2=value2
    etc.

    You could the replace ${mymap} with a map of the specified contents. As starting point you could use the code of PropertyPlaceholderConfigurer.

    Regards,
    Andreas

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    You could also write your own property editor to turn the String into a Map for you.
    http://www.springframework.org/docs/...ans-conversion

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by karldmoore View Post
    You could also write your own property editor to turn the String into a Map for you.
    http://www.springframework.org/docs/...ans-conversion
    Yes, that is also possible. Though, I guess, when placed into a properties file it might become hardly readable for large maps.
    For smaller maps, however, that might indeed by an option.

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    IMHO, I'd go for multiple properties files. It's so much easier then to go with the option I suggested earlier, just load the whole file into a map.

  8. #8
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by karldmoore View Post
    IMHO, I'd go for multiple properties files. It's so much easier then to go with the option I suggested earlier, just load the whole file into a map.
    Yes, indeed

  9. #9
    Join Date
    Aug 2010
    Location
    Barcelona
    Posts
    3

    Default

    Quote Originally Posted by karldmoore View Post
    Couldn't you just do something like this?
    Code:
    	<bean id="myBean" class="myPackage.myClass">
    		<property name="myMap">
    			<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    				<property name="location" value="project.properties"/>
    			</bean>
    		</property>
    	</bean>
    Awesome! Exactly what I was looking to do. Cheers karldmoore!
    Tulio Domingos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •