AOP切面日志通知笔记

1.在切面中,需要通过指定的注解将方法标识为通知方法
   @Before:前置通知的注解,在目标对象方法执行之前执行
   @After:后置通知,在目标对象方法的finally字句中执行的
   @AfterReturning:返回通知,在目标对象方法返回值之后执行
   @AfterThrowing:异常通知,在目标对象发生异常时执行

   
   2.切入点表达式:设置在标识通知的注解的value属性中
   execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int,int))
   execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))
   第一个*表示任意的访问修饰符和返回值类型
   第二个*表示类中的任意的方法
   ..表示任意的参数列表
   类的地方也可以使用*,表示包下的所有的类


   3.重用切入点表达式:
    @Pointcut声明一个公共的切入点表达式
    @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
      public void pointCut(){

      }
      使用方式: @Before("pointCut()"),@After("pointCut()")


   4.获取连接点的信息
  在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
//获取连接点所对应方法的签名信息
  Signature signature = joinPoint.getSignature();
 //获取连接点所对应方法的参数
  Object[] args = joinPoint.getArgs();
@Component
@Aspect     //将当前组件标识为切面
public class LoggerAspect {
    @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
    public void pointCut(){

    }

    //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int,int))")
//    @Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
    @Before("pointCut()")
    public void beforAdviceMethod(JoinPoint joinPoint){
//        获取连接点所对应方法的签名信息
        Signature signature = joinPoint.getSignature();
//       获取连接点所对应方法的参数
        Object[] args = joinPoint.getArgs();

        System.out.println("LoggerAspect,方法"+signature.getName()+",参数:"+ Arrays.toString(args));
    }
@After("pointCut()")
    public void afterAdviceMethod(JoinPoint joinPoint){
    Signature signature = joinPoint.getSignature();

    System.out.println("LoggerAspect,方法:"+signature.getName()+"执行完毕");
    }

//    在返回通知中若要获取对象方法中的返回值,
//    只需要通过@AfterReturning注解的returning属性
//    就可以将通知方法的某个参数指定为接受目标方法的返回值的参数
@AfterReturning(value = "pointCut()",returning = "result")
    public void afterReturningAdvicMethod(JoinPoint joinPoint,Object result){
    Signature signature = joinPoint.getSignature();
    System.out.println("LoggerAspect,方法:"+signature.getName()+",结果:"+result);
    }
    //    在返回通知中若要获取对象方法中的异常,
//    只需要通过@AfterThrowing注解的throwing属性
//    就可以将通知方法的某个参数指定为接受目标方法出现异常的参数
    @AfterThrowing(value = "pointCut()",throwing = "ex")
    public void AfterThrowingAdvicMethod(JoinPoint joinPoint,Exception ex){
        System.out.println("LoggerAspect,方法:"+joinPoint.getSignature().getName()+",异常:"+ex);
    }

    @Around(value = "pointCut()")
public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
        Object proceed = null;
        try {
            System.out.println("环绕通知:");
            //表示目标对象方法的执行
            joinPoint.proceed();
            System.out.println("后置通知"+proceed);
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("异常通知"+e);
        }finally {
            System.out.println("最终通知");
        }
        return proceed;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值