通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer来简化代码,例如:

<bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">
<property name="name" value="zero"/>
<property name="gender">
<value>man</value>
</property>
</bean>

我们希望将name、gender的值写在配置文件中,以后的改动只需要对配置文件进行修改即可,于是就会用到PropertyPlaceholderConfigurer:
test.properties

name=zero
gender=man

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="propertyConf" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="test.properties"/>
</bean> <bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">
<property name="name" value="${name}"/>
<property name="gender">
<value>${gender}</value>
</property>
</bean> </beans>

这里可以更简化我们的配置,使用<context:property-placeholder/>,它会自动加载PropertyPlaceholderConfigurer这个Bean(详细请看源码),于是就有了如下的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="test.properties"/> <bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">
<property name="name" value="${name}"/>
<property name="gender">
<value>${gender}</value>
</property>
</bean>
</beans>

<context:property-placeholder/>其它参数如下:

<context:property-placeholder
location="属性文件,多个之间逗号(,)分隔"
file-encoding="文件编码"
ignore-resource-not-found="是否忽略找不到的属性文件,默认false,即不忽略,找不到将抛出异常"
ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常"
properties-ref="本地Properties配置"
local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反"
system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,OVERRIDE类似于ENVIRONMENT"
order="顺序,当配置多个<context:property-placeholder/>时的查找顺序"
/>

转载于:https://my.oschina.net/zjllovecode/blog/1791907

最新文章

  1. oracle基本操作符/运算符/操作语言
  2. C#获取网页的HTML码、下载网站图片、获取IP地址
  3. Meet Python: little notes 3 - function
  4. freeCodeCamp:Return Largest Numbers in Arrays
  5. cvc-elt.1: 找不到元素 &#39;beans&#39; 的声明
  6. VirtualBox 导入.vdi文件时报“uuid is exists”错误
  7. 3140:[HNOI2013]消毒 - BZOJ
  8. freemarker 数字,字符的操作
  9. ubuntu 自动获取ip的怎么设置
  10. H5微信通过百度地图API实现导航方式二
  11. jmeter 压测最近的心得体会
  12. wince6.0 编译报错:&quot;error C2220: warning treated as error - no &#39;object&#39; file generated&quot;的解决办法
  13. Scrapy 1.4 文档 01 初窥 Scrapy
  14. SpringMVC,SpringBoot利用ajax上传文件到后台
  15. Docker系列学习
  16. IO多路复用的机制:select、poll、epoll
  17. TradeStation简介
  18. Linux应用程序中使用math库报undefined reference to `sin&#39;等
  19. JavaScript基础 :学习javascript的原因
  20. mysql GROUP_CONCAT 用法

热门文章

  1. 在写微信小程序如何 首次编译的是当前写的页面
  2. R语言—如何安装Github包的解决方法,亲测有效
  3. CVPR2020| 阿里达摩院最新力作SA-SSD
  4. TensorFlow保存、加载模型参数 | 原理描述及踩坑经验总结
  5. 011-指针(上)-C语言笔记
  6. (一) Mybatis源码分析-解析器模块
  7. 利用opencv实现视频捕捉功能
  8. 如何把Excel表暴力拆分了,python两段代码帮你搞定
  9. python基础:如何使用python pandas将DataFrame转换为dict
  10. 你的网购价格监督利器——python+爬虫+微信机器人