Announcement Announcement Module
Collapse
No announcement yet.
Define a map in a .properties file using PropertyPlaceholderConfigurer? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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>

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


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

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment


              • #8
                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

                Comment


                • #9
                  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

                  Comment

                  Working...
                  X