dedecms 一键更新网站,2017年网站建设工作总结,重庆做公司网站,郴州网红打卡地有哪些背景#xff1a; 原来在springAOP的用法中#xff0c;只有代理的类才会被切入#xff0c;我们在controller层调用service的方法的时候#xff0c;是可以被切入的#xff0c;但是如果我们在service层 A方法中#xff0c;调用B方法#xff0c;切点切的是B方法#xff0c;…背景 原来在springAOP的用法中只有代理的类才会被切入我们在controller层调用service的方法的时候是可以被切入的但是如果我们在service层 A方法中调用B方法切点切的是B方法那么这时候是不会切入的解决办法就是如上所示在A方法中使用((Service)AopContext.currentProxy()).B() 来调用B方法这样一来就能切入了 原文链接https://blog.csdn.net/qq_29860591/article/details/108728150 https://blog.csdn.net/qq_29860591/article/details/108728150
声明未解决问题先记录场景。
controller层
/*** * author Li* date 2024-03-24*/
RestController
RequestMapping(/peis/student)
public class PeisStudentController extends BaseController
{Autowiredprivate IPeisStudentService peisStudentService;Autowiredprivate IPeisDeptService peisDeptService;Autowiredprivate ApplicationContext applicationContext;/*** 获取部门树列表*/PreAuthorize(ss.hasPermi(peis:student:list))GetMapping(/deptTree)public AjaxResult deptTree(PeisDept dept){logger.info(AopContext.currentProxy().toString());//IPeisDeptService deptService applicationContext.getBean(IPeisDeptService.class);return success(peisDeptService.selectDeptTreeList(dept));}
}
service 层的代码
/*** Service业务层处理* author Li* date 2024-03-24*/
Service
public class PeisDeptServiceImpl implements IPeisDeptService
{protected final Logger logger LoggerFactory.getLogger(this.getClass());Autowiredprivate PeisDeptMapper deptMapper;/*** 查询部门管理数据** param dept 部门信息* return 部门信息集合*/Overridepublic ListPeisDept selectDeptList(PeisDept dept){return deptMapper.selectDeptList(dept);}/*** 查询部门树结构信息** param dept 部门信息* return 部门树信息集合*/Overridepublic ListPeisTreeSelect selectDeptTreeList(PeisDept dept){logger.info(AopContext.currentProxy().toString());//ListPeisDept depts SpringUtils.getAopProxy(this).selectDeptList(dept);ListPeisDept depts ((PeisDeptServiceImpl)AopContext.currentProxy()).selectDeptList(dept);return buildDeptTreeSelect(depts);}
如上所示用到了AopContext.currentProxy() 的方式进行自己类方法的调用实现被代理。代码仿照同结构生成导包无错误注解都加了debug 追踪源码发现之前的代码在controller调用service的时候会进行动态代理类的设值使得currentProxy这个属性放在threadLocal可以被刷新为service类询问gpt得到此步骤一般为spring自动执行。如下图 第二步 但是实际发现 等我研究研究spring深入后再来报仇 先用这个方法解决applicationContext.getBean(IPeisDeptService.class); 原理就是拿到代理对象嘛