zoukankan      html  css  js  c++  java
  • 关于Aop切面中的@Before @Around等操作顺序的说明

    【转】http://www.cnblogs.com/softidea/p/6123307.html

    话不多说,直接上代码:

    1.  
      package com.cdms.aop.aspectImpl;
    2.  
       
    3.  
      import org.aspectj.lang.JoinPoint;
    4.  
      import org.aspectj.lang.ProceedingJoinPoint;
    5.  
      import org.aspectj.lang.annotation.*;
    6.  
      import org.springframework.stereotype.Component;
    7.  
       
    8.  
      import java.util.Arrays;
    9.  
      import java.util.List;
    10.  
       
    11.  
      /**
    12.  
      * 创建 by 草帽boy on 2017/4/1.
    13.  
      */
    14.  
      @Aspect
    15.  
      @Component
    16.  
      public class ICacheAopAction {
    17.  
      @Pointcut("@annotation(com.cdms.aop.ICache)")
    18.  
      private void controllerAspect(){}
    19.  
       
    20.  
      @Before("controllerAspect()")
    21.  
      public void Before(JoinPoint joinPoint){
    22.  
      String classname = joinPoint.getTarget().getClass().getSimpleName();
    23.  
      String methodName = joinPoint.getSignature().getName();
    24.  
      List<Object> args = Arrays.asList(joinPoint.getArgs());
    25.  
      System.out.println("@before Execute! --class name: " + classname + ", method name: " + methodName + " " + args );
    26.  
      }
    27.  
       
    28.  
      @Around("controllerAspect()")
    29.  
      public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    30.  
      System.out.println("@Around:执行目标方法之前...");
    31.  
      Object obj= proceedingJoinPoint.proceed();
    32.  
      System.out.println("@Around:执行目标方法之后...");
    33.  
      System.out.println("@Around:被织入的目标对象为:" + proceedingJoinPoint.getTarget());
    34.  
      System.out.println( "@Around:原返回值:" + obj + ",这是返回结果的后缀");
    35.  
      return obj;
    36.  
      }
    37.  
       
    38.  
      @AfterThrowing("controllerAspect()")
    39.  
      public void AfterThrowing(){
    40.  
      System.out.println("异常通知....");
    41.  
      }
    42.  
       
    43.  
      @After("controllerAspect()")
    44.  
      public void After(JoinPoint point){
    45.  
      System.out.println("@After:模拟释放资源...");
    46.  
      System.out.println("@After:目标方法为:" +
    47.  
      point.getSignature().getDeclaringTypeName() +
    48.  
      "." + point.getSignature().getName());
    49.  
      System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));
    50.  
      System.out.println("@After:被织入的目标对象为:" + point.getTarget());
    51.  
      }
    52.  
       
    53.  
      @AfterReturning("controllerAspect()")
    54.  
      public void AfterReturning(JoinPoint point){
    55.  
      System.out.println("@AfterReturning:模拟日志记录功能...");
    56.  
      System.out.println("@AfterReturning:目标方法为:" +
    57.  
      point.getSignature().getDeclaringTypeName() +
    58.  
      "." + point.getSignature().getName());
    59.  
      System.out.println("@AfterReturning:参数为:" +
    60.  
      Arrays.toString(point.getArgs()));
    61.  
      System.out.println("@AfterReturning:返回值为:" );
    62.  
      System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());
    63.  
      }
    64.  
       
    65.  
      }

      测试的结果是:

    技术分享

    这样就很清楚的看出各种方法是在什么时候调用的啦

  • 相关阅读:
    Android登录界面实现
    博客园自定义模板
    HttpClient + Jsoup模拟登录教务处并获取课表
    sublime编写markdown文件中Ctrl+B的作用
    Java学习路线图
    数学建模比赛论文的基本结构
    GitBash上传代码不计入贡献的问题处理
    Android知识体系图
    Java文件处理:分离全国省市县ID(数据来自和风天气)
    poj3484 Showstopper 二分
  • 原文地址:https://www.cnblogs.com/renjiaqi/p/12021838.html
Copyright © 2011-2022 走看看