比如:UserService
类的某个方法create()
调用了 findById(Long id)
方法,并且findById(Long id)
方法用了spring的缓存。
如果直接用this,或者直接调用,缓存就会失效。这时候可以在UserService
内使用 ((UserService)AopContext.currentProxy())
获取当前类的代理,强制转换为当前类。使用方式如下:
- 配置
expose-proxy="true"
。
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true" />
2、使用代理。代码类型下面。
public class UserServiceImpl implements UserService {
public void create(){
//使用代理
((UserService)AopContext.currentProxy()).findById(1110);
}
@Cacheable(cacheNames=, key="#某个key", unless="#result == null")
public User findById(Long id){
return User
}
}
参考文档: https://blog.youkuaiyun.com/weixin_35796363/article/details/86306291