1、自定义拦截器,将自己的加密业务写入拦截器;
public class EncryptInterceptor extends EmptyInterceptor {
private static final Logger logger = LoggerFactory
.getLogger(EncryptInterceptor.class);
private static final long serialVersionUID = 1L;
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
if (entity instanceof CustInfo) {
EncryptUtil.custInfoEncrypt((CustInfo) entity);
logger.info("onSave");
return true;
} else if (entity instanceof UserInfo) {
EncryptUtil.userInfoEncrypt((UserInfo) entity);
return true;
} else {
return super.onSave(entity, id, state, propertyNames, types);
}
}
}
2、配置拦截器bean到Spring配置文件;
<bean id="encryptIntercept" class="encrypt.interceptor.EncryptInterceptor"/>
3、配置拦截器到Hibernate的配置文件的“sessionFactory”节点;
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="encryptIntercept">encrypt.interceptor.EncryptInterceptor</prop>
</props>
</property>
</bean>
4、运行单元测试,成功入库;