<bean id=“propertyConfig” class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”> web.properties
spring 中这个PropertyPlaceholderConfigurer类就是读取web.properties配置文件中的信息的。所以想要实现这个功能,就需要重写这个类的processProperties方法,并将配置应用到自己的类上。
重新定义的类:EncryptablePropertyPlaceholderConfigurer,并重写processProperties方法,将密码解密,这样生成jdbc对象,链接数据库就可以成功啦。
package cn.mastercom.mtno.comm; import cn.mastercom.mtno.util.DesUtil; import java.util.Iterator; import java.util.Properties; import java.util.Set; import org.apache.log4j.Logger; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private static final String KEY = "Chris "; private static final String IV = “12345678”; protected Logger log = Logger.getLogger(this.getClass()); public EncryptablePropertyPlaceholderConfigurer() { } protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) { try { Set keySet = props.keySet(); String pwdencrypt = GlobalWebSetting.getProperty(“pwdencrypt”, “yes”); Iterator var5 = keySet.iterator(); while(var5.hasNext()) { Object keyObj = var5.next(); String keyStr = (String)keyObj; if (keyStr.contains(“.password”) && “yes”.equals(pwdencrypt)) { String password = DesUtil.decrypt(props.getProperty(keyStr), "Chris ", “12345678”); props.setProperty(keyStr, password);