All Teachers Left Behind High Tech Divining Rod
Sep 20

Spring PropertyPlaceholderConfigurer: A nice clean way to share

Tech Add comments

How many times have you been on a project, and people are talking about where to share configuration data?

Do I use some constants? What about a config file (XML, properties, etc)?

Sometimes it isn’t easy to know what to do, and you sometimes end up with duplicate information.

For example, what if you want to share database information between your code, your ant build, and anything else?

With Spring, you can use their really nice PropertyPlaceholderConfigurer, and easily share a properties file. You can simply share one properties file for all of your build info as well as Spring sharing, or you can of course seperate things out, and have multiple <property file=”filename”/>’s in your build script.

So, the steps for sharing the data:

1. Setup your properties

# DB Info
jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:db/myapp
jdbc.user=sa
jdbc.password=
jdbc.maxConnections=25

2. Setup and use these properties in Spring

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:project.properties</value>
</property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driver}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.user}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

3. Use these properties in Ant

<property file="project.properties"/>

...

<target name="browse">
<java classname="org.hsqldb.util.DatabaseManager" fork="yes" failonerror="true">
<classpath refid="classpath"/>
<arg value="-url"/>
<arg value="${jdbc.url}"/>
</java>
</target>

Easy as pie. Just another small example of how it is a pleasure to work with Spring. I remember futsing around for hours with how to do configuration with JNDI and EJB. What do you put in <env-entry>’s? Ergh. Not anymore!

15 Responses to “Spring PropertyPlaceholderConfigurer: A nice clean way to share”

  1. weathervanes Says:

    Also, another issue which I didn’t mention in my post is regarding theoretical groundings of information science(s)/studies. Here as well, there are theories regarding separate research focuses (information retrieval, information seeking, information behaviors, etc…). However, these sound like theoretical frameworks for various sub-disciplines of information studies rather than Information Science (singular). What is that ‘thing’ that ties all the information sciences (plural) together, besides for the fact that they all claim to be dealing with the ‘thing’ called ‘information’ - which is not necessarily defined the same across the various concentrations and research areas within information science/studies.

  2. John Brinnand Says:

    This info really helps.

    Many thanks

  3. John Brinnand Says:

    This info really helps.

    Many thanks

  4. Shilpa Says:

    Hi,
    How do u fetch a integer value from properties file?

  5. Shilpa Says:

    Hi,
    How do u fetch a integer value from properties file?

    Thanks,
    Shilpa

  6. nayv Says:

    Nice info indeed. One small problem, though: how do you use one PropertyPlaceholderConfigurer over multiple bean definition files in a web application (let’s say you have an xml for acegi related beans, one for domain beans and one for each servlet you proxy through spring)?

  7. cplusstudent Says:

    Great Info. Thanks.

  8. eceppda Says:

    I believe that to use the PropertyPlaceholderConfigurer over multiple bean definition files, you need to add the bean definition in each definition file.

  9. Andrew Boddy Says:

    eceppda: (PropertyPlaceholderConfigurer over multiple…) No, you don’t; use the “locations” property instead of “location”.

  10. rowland Says:

    if you could know where you fall - you would spread some straw (there)

  11. myspace Says:

    myspace [url=http://acc.csueastbay.edu/~csrm/kwiki/plugin/attachments/ITiCSE07csrmWG/Myspace.html#734881141]myspace[/url] http://acc.csueastbay.edu/~csrm/kwiki/plugin/attachments/ITiCSE07csrmWG/Myspace.html 28922356

  12. Anonymous Says:

    Hi,
    Actually I am trying to use PropertyPlaceHolderConfigurer and as described above and in other articles but getting exception: nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not load JDBC driver class [${jdbc.driverClassName}];

    The property file and xml configuration is exact same as what is mentioned in this article. Can you help what is going wrong?
    Thank you.

  13. Pavel Jetensky Says:

    May be you are using BeanFactory, rather than ApplicationContext, which extends BeanFactory? ApplicationContext will detect automatically PropertyPlaceHolderConfigurer as one of it’s beans, but BeanFactory will not.

    If you use bean factory, you can still use it in following way (source http://static.springframework.org/spring/docs/1.2.x/reference/beans.html)

    XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(”beans.xml”));
    // create placeholderconfigurer to bring in some property
    // values from a Properties file
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    cfg.setLocation(new FileSystemResource(”jdbc.properties”));
    // now actually do the replacement
    cfg.postProcessBeanFactory(factory);

  14. Ed Says:

    Pavel your answer was just what I was looking for. Thanks!!

  15. Chandana Says:

    Thanks, this is exactly what I needed.

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'dog'