Spring学习笔记十四--Aspect切面优先级

package aopa;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Aspect
@Component
public class ValidAspect {

    @Before("execution(public int aopa.CalImpl.*(..))")
    public void beforMethod(JoinPoint joinPoint) {
        System.out.println("ValidAspect beforeMethod()....");
    }
}
package aopa;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;

@Order(2)
@Aspect
@Component
public class Logging {

    /*
    //前置通知
    @Before("execution(public int aopa.CalImpl.*(..))")
    public void beforeMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("前置通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args));
    }
    //后置通知,无论该方法是否出现异常
    @After("execution(public int aopa.CalImpl.*(..))")
    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("后置通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args));
    }

    //返回通知,可以访问方法返回值
    @AfterReturning(value = "execution(public  int aopa.CalImpl.*(..))", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("后置返回通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args) + " result : " + result);
    }

    //出现异常时候执行的代码
    @AfterThrowing(value = "execution(public  int aopa.CalImpl.*(..))", throwing = "ex")
    public void afterThrow(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("异常通知 " +  "MethodName : " + methodName + " Exception : " + ex);
    }
    */
    //环绕通知ProceedingJoinPoint参数,是否执行目标方法,必须有目标方法返回值,相当于动态代理
    @Around("execution(public  int aopa.CalImpl.*(..))")
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        Object[] args = proceedingJoinPoint.getArgs();
        System.out.println("AroundMethod ....");
        //执行目标方法
        try {
            //前置通知
            System.out.println("前置通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args));
            result = proceedingJoinPoint.proceed();
            System.out.println("后置返回通知 " + result);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常通知 " +  "MethodName : " + methodName + " Exception : " + throwable);
            throw new RuntimeException();
        }
        System.out.println("后置通知 ");
        return  result;
    }
}


转载于:https://my.oschina.net/jimyao/blog/634837

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值