国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

2019-11-15 00:54:55
字體:
來源:轉載
供稿:網友
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方法就可以。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 茌平县| 汶川县| 甘孜| 桂林市| 新宁县| 大同市| 桃园市| 临潭县| 谷城县| 沂源县| 炎陵县| 曲阳县| 天镇县| 瑞丽市| 清新县| 中山市| 绥芬河市| 北碚区| 金沙县| 无锡市| 襄汾县| 郓城县| 龙陵县| 辽阳市| 建水县| 德钦县| 隆德县| 林西县| 绥化市| 三江| 策勒县| 吉木乃县| 北安市| 平塘县| 团风县| 太仓市| 永昌县| 依兰县| 中牟县| 堆龙德庆县| 清徐县|