首页 > 其他 > 详细

环绕通知@Around

时间:2017-08-29 19:19:05      阅读:867      评论:0      收藏:0      [点我收藏+]

1.环绕通知需要在方法的参数中指定JoinPoint的子接口类型ProceedingJoinPoint为参数
  @Around(value="pointCut()")
  public void around(ProceedingJoinPoint joinPoint){
  }
 2.环绕通知会将其他4个通知能干的,自己都给干了!
  注意:@Around修饰的方法一定要将方法的返回值返回!本身相当于代理!
  
  @Around(value="pointCut()")
  public Object around(ProceedingJoinPoint joinPoint){
   Object[] args = joinPoint.getArgs();
   Signature signature = joinPoint.getSignature();
   String methodName = signature.getName();
   List<Object> list = Arrays.asList(args);
   Object result = null;
   try {
    //目标方法之前要执行的操作
    System.out.println("[环绕日志]"+methodName+"开始了,参数为:"+list);
    //调用目标方法
    result = joinPoint.proceed(args);
    //目标方法正常执行之后的操作
    System.out.println("[环绕日志]"+methodName+"返回了,返回值为:"+result);
   } catch (Throwable e) {
    //目标方法抛出异常信息之后的操作
    System.out.println("[环绕日志]"+methodName+"出异常了,异常对象为:"+e);
    throw new RuntimeException(e.getMessage());
   }finally{
    //方法最终结束时执行的操作!
    System.out.println("[环绕日志]"+methodName+"结束了!");
   }
   return result;
  }

环绕通知@Around

原文:http://www.cnblogs.com/Ysuwade/p/7449804.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!