码迷,mamicode.com
首页 > 编程语言 > 详细

spring如何引用properties文件里的配置

时间:2014-08-06 18:34:21      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   使用   os   

 
1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符
bubuko.com,布布扣<beans>
bubuko.com,布布扣<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
bubuko.com,布布扣           <property name="location"><value>db.properties</value></property>
bubuko.com,布布扣</bean> 
bubuko.com,布布扣<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
bubuko.com,布布扣    <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
bubuko.com,布布扣    <property name="url"><value>${jdbc.url}</value></property>
bubuko.com,布布扣    <property name="username"><value>${jdbc.username}</value></property>
bubuko.com,布布扣    <property name="password"><value>${jdbc.password}</value></property>
bubuko.com,布布扣</bean>
bubuko.com,布布扣</beans>
db.properties文件
bubuko.com,布布扣jdbc.driverClassName=org.hsqldb.jdbcDriver
bubuko.com,布布扣jdbc.url=jdbc:hsqldb:hsql://production:9002
bubuko.com,布布扣jdbc.username=sa
bubuko.com,布布扣jdbc.password=root
2.PropertyOverrideConfigurer类
跟PropertyPlaceholderConfigurer功能一样,不过用法不一样.不用占位符,在属性文件中
直接定义属性的值,这样就允许有默认值
bubuko.com,布布扣<beans>
bubuko.com,布布扣<bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
bubuko.com,布布扣           <property name="location"><value>db.properties</value></property>
bubuko.com,布布扣</bean> 
bubuko.com,布布扣<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
bubuko.com,布布扣    <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
bubuko.com,布布扣    <property name="url"><value>jdbc:hsqldb:hsql://production:9002</value></property>
bubuko.com,布布扣    <property name="username"><value>test</value></property>
bubuko.com,布布扣    <property name="password"><value>123456</value></property>
bubuko.com,布布扣</bean>
bubuko.com,布布扣</beans>
db.properties文件
bubuko.com,布布扣dataSource.username=admin
bubuko.com,布布扣dataSource.password=9527
在bean实例时,admin,9527将替代test,123456
3其他
1)如果需要引用多个属性,将configBean属性改为
bubuko.com,布布扣<property name="locations">
bubuko.com,布布扣<list>
bubuko.com,布布扣<value>db.properties</value>
bubuko.com,布布扣<value>db1.properties</value>
bubuko.com,布布扣</list>
bubuko.com,布布扣</property>
2)在ApplactionContext中是自动调用BeanFactoryPostProcessor接口的,如果要在BeanFactory中使用,必须手动添加:
bubuko.com,布布扣XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
bubuko.com,布布扣PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
bubuko.com,布布扣cfg.setLocation(new FileSystemResource("jdbc.properties"));
bubuko.com,布布扣cfg.postProcessBeanFactory(factory);
bubuko.com,布布扣
 

spring如何引用properties文件里的配置,布布扣,bubuko.com

spring如何引用properties文件里的配置

标签:des   style   blog   http   color   java   使用   os   

原文地址:http://www.cnblogs.com/shijiaoyun/p/3895155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!