Spring配置中引用外部属性文件
2014-01-07 10:14
691 查看
Spring为我们提供了一个PropertyPlaceholderConfigurer,他能够使Bean在配置的时候引用外部属性文件。ropertyPlaceholderConfigurer实现了BeanFactoryPostProcessorBean接口,因此也是一个Bean工厂后处理器。
以前的配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destory-metnod="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:userName="root"
p:password="1234"/>
现在我们可以使用一个jdbc.properties的配置文件:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/sampledb
userName=root
password=1234
属性文件可以定义多个属性,每个属性都由一个属性名和一个属性值组成,两者用“=”隔开。下面通过PropertyPlaceholderConfigurer引入jdbc.properties属性文件,调整数据源Bean的配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:com/baobaotao/placeholder/jdbc.properties"
p:fileEncoding="utf-8"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="&{driverClassName}"
p:url="&{url}"
p:userName="${password}"/>
PropertyPlaceholderConfigurer其他属性:
locations:如果是一个属性文件,用location足以,如果是多个属性文件,locations搞定。
fileEncoding:属性文件的编码格式。
order:如果配置文件中定义了多个PropertyPlaceholderConfigurer,则通过此属性来制定优先顺序。
使用<context:property-placeholder>引用属性文件
<context:property-placeholder location="classpath:com/baobaotao/placeholder/jdbc:properties"/>
当然 还可以基于注解直接给java对象附上属性值,详细的请参看《spring3.x。。实战》
以前的配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destory-metnod="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:userName="root"
p:password="1234"/>
现在我们可以使用一个jdbc.properties的配置文件:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/sampledb
userName=root
password=1234
属性文件可以定义多个属性,每个属性都由一个属性名和一个属性值组成,两者用“=”隔开。下面通过PropertyPlaceholderConfigurer引入jdbc.properties属性文件,调整数据源Bean的配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:com/baobaotao/placeholder/jdbc.properties"
p:fileEncoding="utf-8"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="&{driverClassName}"
p:url="&{url}"
p:userName="${password}"/>
PropertyPlaceholderConfigurer其他属性:
locations:如果是一个属性文件,用location足以,如果是多个属性文件,locations搞定。
fileEncoding:属性文件的编码格式。
order:如果配置文件中定义了多个PropertyPlaceholderConfigurer,则通过此属性来制定优先顺序。
使用<context:property-placeholder>引用属性文件
<context:property-placeholder location="classpath:com/baobaotao/placeholder/jdbc:properties"/>
当然 还可以基于注解直接给java对象附上属性值,详细的请参看《spring3.x。。实战》
相关文章推荐
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
- (五)spring配置文件--使用外部属性文件
- Spring中配置bean时使用外部属性文件
- Springboot引用外部配置文件
- spring学习总结(六):IOC & DI 配置Bean之作用域、加载外部属性文件、SPEL
- Spring 基于注解装配依赖对象和引用外部文件读取数据库配置
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- 说说在Spring中如何引用外部属性文件的方法
- Springboot引用外部配置文件
- Spring引用外部文件生成主键配置
- spring配置文件location引用外部文件
- JdbcTemplae使用入门&&Spring三种连接池配置&&Spring配置文件引用外部properties文件
- Springboot引用外部配置文件
- Spring4笔记----使用外部属性文件配置 bean
- Spring-Hibernate配置中,引用外部property文件
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- springboot 配置文件中属性变量引用方式@@解析
- spring配置文件中引用外部的properties文件
- Spring-Bean配置-使用外部属性文件