application-context.xml 数据源配置
需要注意的是: <property name="properties" ref="dataSourceProperties"/> 需要写在最前面 不然可能会出现密码错误等问题
bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="properties" ref="dataSourceProperties"/>
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<!--<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> -->
<property name="autoCommitOnClose" value="true"/>
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}"/>
<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
</bean>
<bean id="dataSourceProperties" class="com.ezsite.doc.PropertiesEncryptFactoryBean">
<property name="properties">
<props>
<prop key="user">加密后的用户名</prop>
<prop key="password">加密后的密码</prop>
</props>
</property>
</bean>
实现类
需要实现FactoryBean 否则会报数据类型错误
package com.ezsite.doc;
import java.util.Properties;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.FactoryBean;
import sun.misc.BASE64Decoder;
public class PropertiesEncryptFactoryBean implements FactoryBean{
private Properties properties;
public Object getObject() throws Exception {
return getProperties();
}
public Class getObjectType() {
return java.util.Properties.class;
}
public boolean isSingleton() {
return true;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties inProperties) {
this.properties = inProperties;
String originalUsername = properties.getProperty("user");
String originalPassword = properties.getProperty("password");
if (originalUsername != null){
String newUsername = deEncryptPassword(originalUsername );
properties.put("user", newUsername);
}
if (originalPassword != null){
String newPassword = deEncryptPassword(originalPassword);
properties.put("password", newPassword);
}
}
private String deEncryptPassword(String originalPassword){
return deEncryptString(originalPassword);
}
//相关解密操作
private String deEncryptString(String originalString){
return originalString;
}
}