The Spring configuration files can include values from your properties files. You can easily customize this so that you can take control when the key and or the value of a property is requested. To do so, you must first tell Spring the location of your properties file:
<beans>
<bean class="com.mycompany.MyPropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:my.properties</value>
</property>
</bean>
...
Next you must create the class:
com.mycompay.MyPropertyPlaceholderConfigurer:
package com.mycompany;
import java.util.Properties;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.adobe.hs.spdf.common.PropReader;
public class MyPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
// process the key and then return the associated value...
protected String resolvePlaceholder(String placeholder, Properties props) {
String value = process-the-key-and-lookup-the-value;
return value;
}
}
Finally, you can use the Spring’s ${...} notation to reference values from your properties file. e.g.,
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>${mysql.server}</value></property>
<property name="username"><value>${mysql.username}</value></property>
<property name="password"><value>${mysql.password}</value></property>
</bean>
November 4, 2008 at 12:51 pm |
Very helpful. Especially the class writeup.