1、 本类中 方法调用 ,被调用的方法没有进入 切面(即调用的不是 代理类,而是自身的原始类的方法);
public class OrganServiceImpl implements OrganService {
public String getFoo(){
return "此方法 有 aop ";
}
public String getOrganList(){
this.getFoo();
return "此方法 没有 aop ";
}
}
前台 ajax请求 getOrganList() 方法, 切面无效;
如果 是其他类调用 getFoo, 切面有效;
原因:
this.getFoo() ; 方法实际上只是单纯的调用了本类中的方法,并没有调用它的 代理类,所以 无法进入切面,
解决方法:
想办法让 getOrganList() 调用 getFoo() 的代理类;
同理: 本类中 调用 有事务的方法 ,也将导致事务失效;(例:循环 保存)
public class OrganServiceImpl implements OrganService {
public String getFoo(){
return "此方法 有 aop ";
}
public String getOrganList(){
// this.getFoo(); // 调用 本身 的getFoo();
organServiceImpl.getFoo(); // 调用代理类的 getFoo() 方法
return "此方法 没有 aop ";
}
@Autowired
private OrganServiceImpl organServiceImpl;
}
2、异常
can not find proxy: set exposeproxy property on advised to make it available
当需要调用时,则调用接口((BeanClass) AopContext.currentProxy()).B();
如果在配置中将expose-proxy设置为true,则直接获取就可以了:
<aop:config expose-proxy="true">
// spring aop
<!-- 开启暴露Aop代理到ThreadLocal支持 -->
<aop:aspectj-autoproxy expose-proxy="true"/>
https://blog.youkuaiyun.com/hong10086/article/details/78424481
本文深入探讨了在Spring框架中,AOP(面向切面编程)在特定场景下失效的原因,尤其是在同一类内方法调用时,以及如何通过正确地调用代理类来解决问题,避免事务失效。同时,文章提供了配置暴露代理的解决方案。
1446

被折叠的 条评论
为什么被折叠?



