1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符

beans>
Spring 中 properties 的使用<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
Spring 中 properties 的使用           <property name="location"><value>db.properties</value></property>
Spring 中 properties 的使用</bean> 
Spring 中 properties 的使用<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
Spring 中 properties 的使用    <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
Spring 中 properties 的使用    <property name="url"><value>${jdbc.url}</value></property>
Spring 中 properties 的使用    <property name="username"><value>${jdbc.username}</value></property>
Spring 中 properties 的使用    <property name="password"><value>${jdbc.password}</value></property>
Spring 中 properties 的使用</bean>
Spring 中 properties 的使用</beans>

db.properties文件

Spring 中 properties 的使用<beans>
Spring 中 properties 的使用<bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
Spring 中 properties 的使用           <property name="location"><value>db.properties</value></property>
Spring 中 properties 的使用</bean> 
Spring 中 properties 的使用<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
Spring 中 properties 的使用    <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
Spring 中 properties 的使用    <property name="url"><value>jdbc:hsqldb:hsql://production:9002</value></property>
Spring 中 properties 的使用    <property name="username"><value>test</value></property>
Spring 中 properties 的使用    <property name="password"><value>123456</value></property>
Spring 中 properties 的使用</bean>
Spring 中 properties 的使用</beans>

db.properties文件

Spring 中 properties 的使用<property name="locations">
Spring 中 properties 的使用<list>
Spring 中 properties 的使用<value>db.properties</value>
Spring 中 properties 的使用<value>db1.properties</value>
Spring 中 properties 的使用</list>
Spring 中 properties 的使用</property>

2)在ApplactionContext中是自动调用BeanFactoryPostProcessor接口的,如果要在BeanFactory中使用,必须手动添加:

= new XmlBeanFactory(new FileSystemResource("beans.xml"));
Spring 中 properties 的使用PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
Spring 中 properties 的使用cfg.setLocation(new FileSystemResource("jdbc.properties"));
Spring 中 properties 的使用cfg.postProcessBeanFactory(factory);
Spring 中 properties 的使用

 

相关文章: