Announcement Announcement Module
Collapse
No announcement yet.
Using Class Constants in Spring.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Class Constants in Spring.xml

    How do I pass a Class constant to a bean property in a spring.xml file? Here's a sample spring.xml entry illustrating what I'm trying to configure:
    Code:
    <bean id="myBean" class="MyClass">
        <property name="configValue">
            <value>MyOtherClass.CONFIG_VALUE</value>
        </property>
    </bean>
    I found the following Spring classes but don't know how can I use them in the spring.xml file:

    Constants http://www.springframework.org/docs/...Constants.html
    FieldRetrievingFactoryBean file:///home/jojo/java-api/spring-fr...ctoryBean.html

    For me the Constants class fits my need although I'm not sure how to use it in spring.xml. Any tips on how to use it?

  • #2
    The factory can be used like this:

    Code:
    <bean id="myBean" class="MyClass">
        <property name="configValue">
          <bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
              <property name="targetClass">
                 <value>MyOtherClass</code>
              </property>
    
              <property name="targetField">
                 <value>CONFIG_VALUE</code>
              </property>
         </bean>
        </property>
    </bean>
    Note that the classname has to be fully qualified.

    Hope that helps,
    Andreas

    Comment


    • #3
      The ending tag is misspelled ('code' instead of 'value'). Anyway, the value part can be an attribute inside the property tag (unless you'd like to change its type):

      Code:
      <bean id="myBean" class="MyClass">
          <property name="configValue">
            <bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
                <property name="targetClass" value="MyOtherClass"/>
                <property name="targetField" value="CONFIG_VALUE"/>
           </bean>
          </property>
      </bean>
      Last edited by rois_box; Aug 28th, 2013, 05:59 AM. Reason: Haven't formatted the code

      Comment

      Working...
      X