Spring 利用PropertyPlaceholderConfigurer占位符
转载地址:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html
Spring 利用PropertyPlaceholderConfigurer占位符
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:

- <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>conf/sqlmap/jdbc.properties</value>
- </property>
- <property name="fileEncoding">
- <value>UTF-8</value>
- </property>
- </bean>

当然也可以引入多个属性文件,如:

- <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>/WEB-INF/mail.properties</value>
- <value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法
- </list>
- </property>
- </bean>

3.譬如,jdbc.properties的内容为:

- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round;
- jdbc.username=root
- jdbc.password=123456

4.那么在spring配置文件中,我们就可以这样写:

- <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath: conf/sqlmap/jdbc.properties </value>
- </list>
- </property>
- </bean>
- <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <property name="driverClassName"value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}"/>
- <property name="password"value="${jdbc.password}" />
- </bean>

5.这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
6.查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。
PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。
我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 31 | 1 | |||
2 | 3 | 4 | 5 | 6 | 7 | 8 | |||
9 | 10 | 11 | 12 | 13 | 14 | 15 | |||
16 | 17 | 18 | 19 | 20 | 21 | 22 | |||
23 | 24 | 25 | 26 | 27 | 28 | 29 | |||
30 | 1 | 2 | 3 | 4 | 5 | 6 |
搜索
我的标签
随笔档案
最新评论
- 1. Re:调用Window Media Player 播放器代码
- 十分感谢博主提供的代码! 灰常有效!
- --野生程序员
- 2. Re:jquery实现数组array、集合list、json类型的分页
- Jquery ajax传递对象(数组)到后台,直接映射实体就行
- --凤凰涅磐360yi
- 3. Re:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- max_connect_errors?
max_connection_errors?
为什么我的是max_connect_errors? - --liuweishow
阅读排行榜
- 1. MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法(50143)
- 2. MyEclipse设置编码方式(39715)
- 3. VirtualBox 安装 ubuntu 出现This kernel requires an X86-64 CPU,but only detected an i686 CPU错误的解决办法(31164)
- 4. Spring的PropertyPlaceholderConfigurer应用(16224)
- 5. Mysql 1366 中文显示错误修改方法(14729)
Spring 利用PropertyPlaceholderConfigurer占位符的更多相关文章
- Spring PropertyPlaceholderConfigurer占位符用法
1.PropertyPlaceholderConfigurer是一个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现.PropertyPlacehol ...
- SPRING多个占位符配置文件解析源码研究--转
原文地址:http://www.cnphp6.com/archives/85639 Spring配置文件: <context:property-placeholder location=&quo ...
- Spring的PropertyPlaceholderConfigurer应用
Spring 利用PropertyPlaceholderConfigurer占位符 1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFa ...
- Spring的PropertyPlaceholderConfigurer应用(转)
转自:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html Spring 利用PropertyPlaceholderConfigu ...
- 深入Spring Boot:那些注入不了的Spring占位符(${}表达式)
Spring里的占位符 spring里的占位符通常表现的形式是: <bean id="dataSource" destroy-method="close" ...
- Spring中手动增加配置文件中占位符引用的变量
在项目中遇到一个这样的需求,项目的配置文件由外部传入,这时spring配置文件那些占位符变量该如何取值呢? 解决这个问题的做法有几种,我想到的大概有以下三种: 1.通过系统属性来实现,把外部传入的配置 ...
- 深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )
Spring里的占位符 spring里的占位符通常表现的形式是: 1 2 3 <bean id="dataSource" destroy-method="close ...
- sprintboot 中占位符及多环境配置
(原) 关于springboot中多环境配置问题 1.在application.properties文件中通过 spring.profiles.active=... 选择系统所要加载的配置文件,这里的 ...
- 聊聊 SpringBoot 中的两种占位符:@*@ 和 ${*}
前言 在 SpringBoot 项目中,我们经常会使用两种占位符(有时候还会混用),它们分别是: @*@ ${*} 如果我们上网搜索「SpringBoot 的占位符 @」,大部分答案会告诉你,Spri ...
随机推荐
- Extjs相关知识点梳理
store是一个为Ext器件提供record对象的存储容器,行为和属性都很象数据表 方法:不列举继承来的方法 Store( Object config ) 构造,config定义为{ autoLo ...
- asp.net web api 跨域,带cookie
官网上有一个介绍 http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 但是只支 ...
- unity 中的协程
//The coroutine will continue after all Update functionshave been calledon the next frame. ; //Conti ...
- vb.net的String类型和Bytes转换(C#也适用)
1.Bytes---->StringSystem.Text.Encoding.Unicode.GetString(bytes, 0, bytes.Length) 2.String----> ...
- mysql sql语句大全(MySQL语句 整理一)
1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...
- 控制器pop时没有被销毁(没有走dealloc方法)错误原因
ARC环境下,不需要我们进行过多的内存的管理我们需要做的就是在dealloc方法中进行内存管理,但是错误的代码也会造成内存管理方法dealloc不执行,错误的原因无非以下三种,其中第二种和第三种最容易 ...
- BAT技术需求,你能达到多少?
作为中国互联网界的传奇和标杆企业,BAT 三家公司的一举一动受互联网人的精密亲密关注.进入 BAT 成为大厂的一员成了许多互联网人职业生活生存追逐的方针之一. 本文的作者作为一个非科班毕业,出身于三流 ...
- 使用vue+webpack打包时,去掉资源前缀
在build文件夹下找到webpack.prod.conf.js文件,搜索 filename: utils.assetsPath('css/[name].[contenthash].css'), 将[ ...
- springboot 文件上传和下载
文件的上传和下载 1.文件上传 html页面代码如下 <form method="post" action="/file/upload1" enctype ...
- 关于CSS重要知识点(1)
1. 盒子模型 CSS处理网页内容时,会把每一个元素"放在"一个盒子里,也就是所谓的盒子模型. 盒子模型包括4部分:内容,内边距(padding),边框(border)和外边距(m ...