国产探花免费观看_亚洲丰满少妇自慰呻吟_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方法就可以。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会理县| 英超| 武汉市| 辉南县| 龙江县| 东乡族自治县| 承德县| 门头沟区| 商洛市| 鄂托克旗| 乃东县| 博湖县| 凤庆县| 桐庐县| 城步| 澄迈县| 长治县| 饶河县| 鲁甸县| 陆良县| 南投县| 霞浦县| 黄冈市| 晴隆县| 红河县| 安顺市| 石城县| 海丰县| 天长市| 南汇区| 河东区| 武陟县| 永丰县| 台中县| 胶州市| 古浪县| 泽库县| 高州市| 五华县| 抚远县| 抚宁县|