Thursday, June 19, 2008

Reading from Properties file into Spring Application Context.xml

Problem: How to read a value from your properties file into your spring's applicationContext.xml..?

Here is how you do it. spring's angle brackets are replaced with []

[bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"]
[property name="location" value="classpath:application.properties"/]
[/bean]

[!-- A sample bean that needs some settings. --]
[bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"]
[property name="driverClassName" value="${jdbc.driver}"/]
[property name="url" value="${jdbc.url}"/]
[property name="username" value="${jdbc.username}"/]
[property name="password" value="${jdbc.password}"/]
[/bean]


Ex: If your properties file is :

jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:mem:example
jdbc.username=sa
jdbc.password=asdfa

you can these values directly into ur application ctxt.

More info read:

1 comment:

david santos said...

I loved this post and this blog.
Happy day