Announcement Announcement Module
Collapse
No announcement yet.
Accessing property value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing property value

    Hi!

    Often, I would like to use a specific property from a property file in my bean definition (using Spring 2.0.4).

    If I use PropertyPlaceholderConfigurer, I will need to come up with a unique prefix/postfix, so as not to interfere with the rest of the configuration. This is not very encapsulated, and I may end up with a lot of $[], ${}, @[], @{}, #{}, #[] etc, if I need to do it several times. Can I scope the PropertyPlaceholderConfigurer to only process certain bean definitions?

    Alternatively, I can use a MethodInvokingBeanFactory to call getProperty on the properties bean. But that is quite verbose (see the javadoc for MethodInvokingFactoryBean for a sample).

    I ended up writing my own PropertyExtractorBeanFactory, to be used as:

    Code:
    <bean id="myKey" class="util.PropertyExtractorBeanFactory">
        <property name="properties">
            <util:properties id="myProps" location="props.properties"/>
        </property>
        <property name="key" value="myKey"/>
        <property name="defaultValue" value="myDefault"/>
    </bean>
    Is there a simpler approach, using the standard beans?

  • #2
    You can accomplish something similar with factory-bean/factory-method bean attributes:
    Code:
    <util:properties id="myProps" location="props.properties"/>
    <bean id="myFile" factory-bean="myProps" factory-method="getProperty">
        <constructor-arg>myKey</constructor-arg>
        <constructor-arg>defaultValue</constructor-arg>
    </bean>
    I don't really see it as any shorter than yours. But at least you wouldn't need an additional class.

    Comment

    Working...
    X