Spring配置文件使用占位符配置
Spring利用PropertyPlaceholderConfigurer占位符
1、Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中一些动态设定的值(value),在xml中替换为占位该键($key$)的值,.properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性。
2、在Spring中,使用PropertyPlaceholderConfigurer可以在xml配置文件中加入外部属性文件
2.1、可以指定外部文件的编码(location),如:
<pre name="code" class="html"><bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><value>classpath:/spring/include/dbQuery.properties</value></property><property name="fileEncoding"><value>UTF-8</value></property>
</bean>
2.2、引入多个属性文件(locations),如:
<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>
其中classpath是引用src目录下的文件写法。
2.3、接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下:
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="1" /><property name="ignoreUnresolvablePlaceholders" value="true" /><property name="location"><value>classpath:/spring/include/dbQuery.properties</value></property>
</bean>
<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="2" /><property name="ignoreUnresolvablePlaceholders" value="true" /><property name="locations"><list><value>classpath:/spring/include/jdbc-parms.properties</value><value>classpath:/spring/include/base-config.properties</value></list></property>
</bean>
其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true
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="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中定义的工具。
Spring配置文件使用占位符配置相关推荐
- Spring import配置文件使用占位符
转载自 Spring import配置文件使用占位符 import使用占位符 连接池切换导入配置的代码: <import resource="classpath:META-INF/sp ...
- spring 属性占位符_Spring属性占位符配置器–一些不太明显的选项
spring 属性占位符 Spring的PropertySourcesPlaceholderConfigurer用于从XML或Java Config中定义的Spring bean定义外部化属性. Pl ...
- Spring属性占位符配置器–一些不太明显的选项
Spring的PropertySourcesPlaceholderConfigurer用于从XML或Java Config中定义的Spring bean定义外部化属性. PlaceholderConf ...
- 【Android Gradle 插件】ProductFlavor 配置 ( ProductFlavor#manifestPlaceholders 清单文件占位符配置 )
文章目录 一.ProductFlavor#manifestPlaceholders 清单文件占位符配置 Android Plugin DSL Reference 参考文档 : 文档主页 : https ...
- SPRING多个占位符配置文件解析源码研究--转
原文地址:http://www.cnphp6.com/archives/85639 Spring配置文件: <context:property-placeholder location=&quo ...
- Spring配置文件中关于约束配置详解
一.Spring配置文件常见的配置头 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...
- android 渠道包 占位符配置,Android打包
8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 打包的事,之前很少做,也了解的比较少,今天好好看了看,总结一下,以后要是用到了,或许可以少走些弯路. 多渠道打包 友盟多 ...
- Spring配置文件beans.xml头部配置解释
比如一个标准的beans.xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- Spring配置文件-Bean生命周期配置(init-method方法,destory-method方法)
1.UserDaoImpl类 public class UserDaoImpl implements UserDao {public UserDaoImpl(){System.out.println( ...
最新文章
- 基于ECS部署LAMP环境实验记录
- 你的gpu驱动程序不满足_英特尔图形驱动程序现在在Windows/Linux之间共享约60%的代码库...
- 虚拟局域网软件开源_ZeroTier虚拟局域网免费远程桌面体验--替代TeamViewer
- centos 上安装bugzilla 详解
- java关键字static_Java中的static关键字
- XE5 搭建DataSnap服务
- 使用 wizNote 作为 Metaweblog 客户端 在博客园发布博客
- php日文文件名 liunx,linux文件名乱码
- 给我一个支点,我要撬动地球。
- 工业互联网的内涵及其应用
- java实现数字金额转换成汉字大写金额
- do-while循环
- Grapher中如何保存图形模板,批量处理相同格式的文件
- 区块链的前世今生:为什么说区块链是即将到来的数字革命?
- GO语言GET安装问题{cccccccccccccccccccc}
- NE555 + CD4017流水灯
- Junction.exe 命令应用详解
- CString , COleDateTime, CTime相互转换和对时间的一些处理
- 基于three.js如何在模型加消息提示框?
- 学习Python想放弃,因为你没有培养自己的兴趣!
热门文章
- 一对一直播名列直播行业前茅凭的是什么?
- 不中听的意见,不想采纳怎么办?
- 【Unity3D】世界坐标系 ( 左手坐标系 | 游戏物体坐标 | 世界坐标系方向 )
- SAP报工后台配置字段描述
- html 适配iphonex,有关HTML5页面在iPhoneX适配问题
- 3、HFSS建模操作
- HFSS入门学习(二)
- python数据分析之json文件
- 抖音上测试笑容多少分的什么软件,抖音微笑打分app-抖音微笑打分软件app手机版预约 v1.0-68游戏网...
- 2021美赛题目翻译汇总