Announcement Announcement Module
Collapse
No announcement yet.
Referencing collections - props Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referencing collections - props

    The Spring IoC container documentation (3.3.2.4. Collections) defines four supported collection types: list, set, map, and props. Each of these types allows both setting static values and references to other beans within the context... except for props. There doesn't seem to be a way to set a reference when using props:

    A list referencing "myDataSource"
    Code:
    <property name="someList">
      <list>
        <value>a list element followed by a reference</value>
        <ref bean="myDataSource" />
      </list>
    </property>
    A map referencing "myDataSource".
    Code:
    <property name="someMap">
      <map>
        <entry>
          <key>
            <value>an entry</value>
          </key>
          <value>just some string</value>
        </entry>
        <entry>
          <key>
            <value>a ref</value>
          </key>
          <ref bean="myDataSource" />
        </entry>
      </map>
    </property>
    A set referencing "myDataSource".
    Code:
    <property name="someSet">
      <set>
        <value>just some string</value>
        <ref bean="myDataSource" />
      </set>
    </property>
    Notice no reference to "myDataSource" for props
    Code:
    <property name="adminEmails">
      <props>
        <prop key="administrator">[email protected]</prop>
        <prop key="support">[email protected]</prop>
        <prop key="development">[email protected]</prop>
      </props>
    </property>
    Is there a way to reference another bean using the props tag? If not, is this something that could be added in a future release?

  • #2
    Properties only support Strings. Why do you need a ref for a String property when there are already other ways to do this?

    Comment


    • #3
      Well, actually... Properties are not limited to just Strings (though granted, that's how they are typically used).

      If you look at the java.util.Properties JavaDoc API, you'll notice that Properties extends Hashtable<Object, Object>.

      Comment


      • #4
        The Properties object is flawed by design due to the fact that it extends Hashtable. Its purpose is to be only for String properties however they cannot change it anymore.

        As stated by the javadoc from the Properties class itself

        Code:
         * The <code>Properties</code> class represents a persistent set of
         * properties. The <code>Properties</code> can be saved to a stream
         * or loaded from a stream. Each key and its corresponding value in
         * the property list is a string.
        Even worse the Properties object in a lot of methods filters non string based properties, leading to inconsistent/unexpected results. The book 'Effective Java' contains a more detailed section about the Properties object.

        As Spring follows this usage you can only use Strings as properties (and keys). For anything else use plain Map instance.
        Last edited by Marten Deinum; Nov 24th, 2008, 01:21 PM.

        Comment


        • #5
          Ok I see where you are getting that properties are String only... the spring-beans.dtd states:
          Props elements differ from map elements in that values must be strings. Props may be empty.
          So I guess I can't use props to populate a java.util.Properties object that references non-String objects.
          Last edited by Stephen Woods; Nov 24th, 2008, 01:27 PM.

          Comment


          • #6
            Originally posted by Stephen Woods View Post
            Ok I see where you are getting that properties are String only... the spring-beans.dtd states:


            So I guess I can't use props to populate a java.util.Properties object that references non-String objects.
            Right...a Properties object is simply a specialized Hashtable that supports a more explicit interface (e.g. getProperty(), setProperty()) for String values. But that means that it supports all the generic object methods in the Hashtable as well.

            Think about it this way: a properties file can't store anything except String key/value pairs even though a Properties object can, via the Hashtable interface.

            Comment


            • #7
              Ok that makes sense now... thanks for the clarification!

              Comment


              • #8
                Originally posted by chudak View Post
                Properties only support Strings. Why do you need a ref for a String property when there are already other ways to do this?
                Sometimes you may want to use the value of a bean or bean-property as the value for some "props" property. As long as this value is-a-String it is ok.

                Although the original poster has probably already forgotten, there is a solution to this problem - use a <map> instead of <props> :P
                It surprised me as well, but it just works:

                So you can safely replace
                Code:
                	<props>
                		<prop key="foo">blahhhh</prop>
                		<prop key="bar">arrrrgh</prop>
                	</props>
                With
                Code:
                	<map>
                		<entry key="foo" value="blahhhh"/>
                		<entry key="bar">
                			<bean class="java.lang.String">
                				<constructor-arg value="arrrrgh"/>
                			</bean>
                		</entry>
                
                		<!-- and you can even do -->
                		<entry key="baz" value-ref="someBean"/>
                	</map>

                Comment


                • #9
                  I just want to thank eran_ha for that post...what a lifesaver!

                  Comment


                  • #10
                    LOL, thanks @hdave. It saved my life as well

                    Comment

                    Working...
                    X