SpringAOP是Spring中除了依赖注入之外最核心的功能。其原理是利用CGlib和JDK动态代理实现运行时动态方法增强,从而降低系统耦合,提高代码复用性。然而,在享受AOP强大功能和便捷的同时,我们也会经常遇到一些看似莫名其妙的bug。今天我们就来说说为什么在AOP的方法中,不用this来方便的调用方法?使用它时会发生什么?其背后的原理是什么?如何解决?废话不多说,直接上实际代码。场景复现假设我们有一个核心支付类,它有pay()支付功能,会通过record()方法记录哪些用户访问了这个核心功能。@ServicepublicclassPayService{publicvoidpay(){System.out.println("执行一些核心支付业务操作");//记录用户访问日志this.record();}publicvoidrecord(){try{System.out.println("模拟将操作记录发布到日志系统需要100ms");TimeUnit.MICROSECONDS.sleep(100);}catch(InterruptedExceptione){thrownewRuntimeException(e);随着业务的不断扩大,我们需要统计保存访问日志这个耗时动作,看是否会对核心支付功能产生更大的影响。因此,我们使用SpringAOP进行切面处理。@Aspect@ComponentpublicclassLogAspect{@Around(value="execution(*com.shishan.demo2023.service.PayService.record()))")publicObjectrecord(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{longbegin=System.currentTimeMillis();对象proceed=proceedingJoinPoint.proceed();System.out.println("日志耗时:"+(System.currentTimeMillis()-begin));返回进行;}}aspect类很简单,通过@Around方法切入切出record方法,记录方法的执行时间。看起来很完美不是吗?旧的代码不需要改动,只需要增加一个新的切面就可以满足新的需求。让我们创建一个新的控制器,看看我们的切面类是否生效。@RestController@RequestMapping(value="/demo")publicclassDemoController{@ResourceprivatePayServicepayService;@RequestMapping(value="/pay")publicResponseEntity
