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!