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!

30 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.

  16. Rajeev Says:

    Is there a way to use a property file which resides outside my classpath (some where in my file system say in C: drive of windows system) to be used with PropertyPlaceholderConfigurer?

  17. Andres Says:

    Excellent article. It took me a while to discover that the problem was that I was using a BeanFactory instead of an ApplicationContext class.

    thanks!!!

  18. Andros Says:

    @ Rajeev: I’d like to know the same thing.. I want to use config file in /etc/module/config.properties

  19. Andros Says:

    I found the answer.. use file:// URL … so file:///etc/module/config.properties

  20. Mike Jackson Says:

    So, referring to Pavel’s solution, what we have in our hands is a spring configuration that may or may not work as desired depending on the code that happens to read it.

    What shall we call this paradigm? I propose either ‘Inversion of Inversion of Control’ or (my favourite) ‘Dependency Rejection’

    ;-)

  21. sudhir Says:

    Thanks for the post.. It helped..

  22. bwestendorf Says:

    I am getting the property name instead of value:

    Account Info: ${sample.accountNumber}

    Any ideas what I am missing? Where in the project does the prop file go?

    classpath:sample.properties

    thanks, brian

  23. sai Says:

    Thanks, man. Very helpful. You saved the day

  24. Krishan Says:

    Is there a way using PropertyPlaceholderConfigurer to define Env dependetnt properties file, e.g Lets say we have three files 1) dev_db.properties 2) test_db.propertie 3) prod_db.properties.

    Then we can set the property file in or application depending upon the env in which application is deployed.

  25. Dibya Says:

    Thanks Pavel .

  26. Moaad Says:

    There’s a way to use a a properties file on the file system with a relative path ?
    something like : file:///./module/config.properties ?
    thanks in advance

  27. Leon Says:

    Looks like everybody is looking for this:
    instead of catalina.home you can enter any variable which is loaded at startup. ( -D xxx)

  28. Leon Says:

    Added the xml markers yourself

    bean id=”propertyConfigurer”
    class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”
    property name=”location” value=”file:${catalina.home}\conf\config.properties”/
    /bean

  29. ignatius Says:

    Define a properties file like this:

    environment=development

    development.jdbc.url=localhost….
    production.jdbc.url=server.com
    test.jdbc.url=otherserver

    jdbc.url=${${enviroment}.jdbc.url}

    and to use it in the xml: ${jdbc.url} and to change the enviroment, just change “enviroment” to test or production

  30. lwpro2 Says:

    is that compulsory to put the propertyplaceholderconfigurer there, if I want to use the passed in properties file?

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 'ajax'