宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

AOP环绕通知引起的java.lang.ClassCastException

2023年11月3日 1838点热度 0人点赞 0条评论

原来报错的代码:

@Aspect
@Component
public class MyLog {
//创建日志对象
Logger logger=Logger.getLogger(MyLog.class);
@Pointcut("execution(* *.*.service.Impl.*.*(..))")
public void method(){}
@Around("method()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
long time = new Date().getTime();
joinPoint.proceed();
long time1 = new Date().getTime();
long l = time1 - time;
logger.info(logger.getName()+"方法调用完成的时间为:"+l+"毫秒");
return joinPoint;
}
}

在这里插入图片描述
报错原因直接返回了joinPoint,修改后的代码:

@Aspect
@Component
public class MyLog {
//创建日志对象
Logger logger=Logger.getLogger(MyLog.class);
@Pointcut("execution(* *.*.service.Impl.*.*(..))")
public void method(){}
@Around("method()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
long time = new Date().getTime();
Object proceed = joinPoint.proceed();//获得当前对象的反射克隆对象,反射执行需要执行的方法
long time1 = new Date().getTime();
long l = time1 - time;
logger.info("方法调用完成的时间为:"+l+"毫秒");
return proceed;
}
}

在这里插入图片描述

标签: 暂无
最后更新:2023年11月3日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2