您的位置:首页 > 编程语言 > Java开发

【spring】 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

2016-12-23 21:12 363 查看
原文链接

可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。

从上图中,我们看到PropertyPlaceholderConfigurer实现了三个bean生命周期的接口:BeanFactoryAware & BeanNameAware & BeanFactoryPostProcessor。关于spring bean的生命周期,可以参考这里http://blog.csdn.net/gjb724332682/article/details/46767463

PropertyResourceConfigurer.postProcessBeanFactory()将properties文件中的属性进行merge,convert,最后调用PropertyPlaceholderConfigurer.processProperties()完成遍历bean定义替换属性占位符。

例子:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/conf/xx.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>
</list>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring xml bean
相关文章推荐