java 横切_java--- AOP切面编程注解示例:纵向执行的程序类是谁,你将从哪里进行横切,怎么切,切点如何建立...

packagecom.example.demo.aop01;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.After;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;/*** 通知类,横切逻辑*/@Component

@Aspectpublic classAdvices {//切点

@Pointcut("execution(* com.example.demo.aop01.Math.a*(..))")public voidpointcut(){

}//前置通知

@Before("pointcut()")public voidbefore(JoinPoint jp){

System.out.println(jp.getSignature().getName());

System.out.println("----------前置通知----------");

}//最终通知

@After("pointcut()")public voidafter(JoinPoint jp){

System.out.println("----------最终通知----------");

}//环绕通知

@Around("execution(* com.example.demo.aop01.Math.s*(..))")public Object around(ProceedingJoinPoint pjp) throwsThrowable{

System.out.println(pjp.getSignature().getName());

System.out.println("----------环绕前置----------");

Object result=pjp.proceed();

System.out.println("----------环绕后置----------");returnresult;

}//返回结果通知

@AfterReturning(pointcut="execution(* com.example.demo.aop01.Math.m*(..))",returning="result")public voidafterReturning(JoinPoint jp,Object result){

System.out.println(jp.getSignature().getName());

System.out.println("结果是:"+result);

System.out.println("----------返回结果----------");

}//异常后通知

@AfterThrowing(pointcut="execution(* com.example.demo.aop01.Math.d*(..))",throwing="exp")public voidafterThrowing(JoinPoint jp,Exception exp){

System.out.println(jp.getSignature().getName());

System.out.println("异常消息:"+exp.getMessage());

System.out.println("----------异常通知----------");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值