缘由 由于要对数据库的部分数据做缓存,并且没有现成的缓存与数据库的一致性的保障机制,所以只能在DAO对数据库操作的同时更新缓存。 为了避免对原有代码的侵入,决定采用spring的aop拦截DAO对数据库的操作然后更新缓存。 分析 尝试 一开始,我实现了如下的代码,对删除进行拦截: @AfterReturning ( pointcut = "execution(public * dao.AccountDao+.deleteX(..))" , returning = "affectedRowCount" ) public void delete (JoinPoint joinPoint, int affectedRowCount) { if (affectedRowCount == 1 ) { xCache.delete((Integer) joinPoint.getArgs()[ 0 ]); } } 但是测试发现,拦截并没有成功。 深入 经检查,spring的配置没有问题,aop的语法也是正确的。那为什么拦截不到呢? 于是怀疑跟AccountDao这个interface有关: public interface AccountDao extends GenericDao { @DAOAction (action = DAOActionType.UPDATE) public int delete (@ DAOParam ("accountId") int accountId); //... } 仔细查看源码,发现这个interface其实并没有实现类!也就是说,这个interface或者他内部的方法是动态生成的。 查看这个bean的定义如下: < bean id = "accountDao" parent = "parentDao" > < property name = "proxyInterfaces" ...
Learn programming, still on the way