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

  • Accessing property value


    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:

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

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