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