Spring- 5AOP

面向切面编程的一种方式
1 配置扫描
在这里插入图片描述
2 通过注解切面
在这里插入图片描述

 */
@Aspect
@Component
public class Aop {

    private void aop(JoinPoint joinPoint, String s) {
        Signature signature = joinPoint.getSignature();
        String methonName = signature.getName();
        Object[] args = joinPoint.getArgs();
        System.out.println(getClass().getName() + s + ",方法名字:" + methonName + ",参数列表:" + List.of(args));
    }

    @Pointcut(value = "execution( public *  spring.anntation.aop.dynamicProxy.CalculateImpl.*(..))")
    private void beforeDoPoint() {
    }

    @Around(value = "beforeDoPoint()")
    private Object around(ProceedingJoinPoint proceedingJoinPoint) {
        System.out.println("环绕执行方法前");
        Signature signature = proceedingJoinPoint.getSignature();
        //方法名字
        String mothodName = signature.getName();
        //参数列表
        Object[] args = proceedingJoinPoint.getArgs();

        Object result = null;
        try {
            //方法执行
            result = proceedingJoinPoint.proceed(args);
            System.out.println("环绕执行方法返回" + result);
        } catch (Throwable e) {
            e.printStackTrace();
            String message = e.getMessage();
            if (message == null) {
                message = e.getCause().getMessage();
            }
            System.out.println("环绕方法异常" + message);
            //抛出去让afterThrowing捕捉
            throw new RuntimeException(message);
        } finally {
            System.out.println("环绕方法后置");
        }
        return result;

    }

    @AfterThrowing(value = "beforeDoPoint()", throwing = "e")
    private void beforeThrow(JoinPoint joinPoint, Exception e) {
        aop(joinPoint, "异常通知");
        System.out.println(e);
//        throw new RuntimeException(e);
    }

    @AfterReturning(value = "beforeDoPoint()", returning = "result")
    public Object afterReturn(JoinPoint joinPoint, Object result) {
        aop(joinPoint, "返回通知");
        System.out.println("返回通知" + result);
        return result;
    }

    @After(value = "beforeDoPoint()")
    public void afterDo(JoinPoint joinPoint) {
        aop(joinPoint, "后置通知");
    }


    @Before(value = "beforeDoPoint()")
    public void beforeDo3(JoinPoint joinPoint) {
        aop(joinPoint, "前置通知");
    }

//    @Before(value = "execution(public  int  spring.anntation.aop.dynamicProxy.CalculateImpl.add(int, int))")
//    public void beforeDo2(JoinPoint joinPoint) {
//        Signature signature = joinPoint.getSignature();
//        String methonName = signature.getName();
//        Object[] args = joinPoint.getArgs();
//        System.out.println(getClass().getName() + "前置通知,方法名字:" + methonName + ",参数列表:" + List.of(args));
//    }


}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值