org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

org.sPRingframework.beans.factory.config.PropertyPlaceholderConfigurer

可以将上下文(配置文件)中的属性值放在另一个单独的标准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>

<bean id="econsoleDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass"><value>${jdbc.driverClassName}</value></property><property name="jdbcUrl"><value>${jdbc.url}</value></property><property name="user"><value>${jdbc.username}</value></property><property name="passWord"><value>${jdbc.password}</value></property></bean>

除此之外,我们还可以扩展自这个类,用来诸如加解密配置信息等操作。如下:

import java.util.Properties;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanInitializationException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import com.xxx.util.AESUtils;public class DecryptPropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer {private String key = "xxxxxx";@Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)throws BeansException {try {String driverClassName = props.getProperty("driverClassName");if (driverClassName != null) {props.setProperty("driverClassName",AESUtils.aesDecrypt(driverClassName, key));}String url = props.getProperty("url");if (url != null) {props.setProperty("url", AESUtils.aesDecrypt(url, key));}String username = props.getProperty("username");if (username != null) {props.setProperty("username",AESUtils.aesDecrypt(username, key));}String password = props.getProperty("password");if (password != null) {props.setProperty("password",AESUtils.aesDecrypt(password, key));}super.processProperties(beanFactory, props);} catch (Exception e) {e.printStackTrace();throw new BeanInitializationException(e.getMessage());}}}

重写processProperties方法就可以。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航