spring之外部化Bean配置

spring之外部化Bean配置

----------

 

在配置文件里配置Bean时,必须牢记在Bean的配置里混入系统部署的细节信息并不是好的做法。这些信息包括文件路径、服务器地址、用户名以及密码。通常,应用程序开发人员负责编写Bean的配置,而部署人员或者系统管理员负责管理系统部署的细节。

Spring提供了一个叫做PropertyPlaceholderConfigurer的Bean Factory后置处理器,这个处理器允许用户将Bean配置的部分内容外部化到属性文件里。可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer将从属性文件里加载属性,并使用这些属性来替换变量

如例:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location">
		<value>config.properties</value>
	</property>
</bean>
作为Bean Factory后置处理器,PropertyPlaceholderConfigurer将会在Bean被实例化之前用外部的属性替换掉Bean配置文件里的相应变量。

Spring2.5里,可以通过<context:property-placeholder>元素简化PropertyPlaceholderConfigurer的注册。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframeork.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
		
	<context:property-placeholder location="config.propertis"/>
	...
</beans>

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值