您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 1浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【精品】Spring AOP 基于AspectJ实现 注解和XML形式

梁云亮 发布时间:2021-11-17 10:17:58 ,浏览量:1

第一步:添加Maven依赖


    org.aspectj
    aspectjrt
    ${aspectjrt.version}


    org.aspectj
    aspectjtools
    ${aspectjrt.version}


    org.aspectj
    aspectjweaver
    ${aspectjrt.version}


    aopalliance
    aopalliance
    ${aopalliance.version}

第二步:创建待增强的接口及实现类 Calculator.java
public interface Calculator {
    int add(int a, int b);
    int sub(int a, int b);
    int div(int a, int b);
    int mut(int a, int b);
}
CalculatorImpl.java
@Component("calculator")
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int a, int b) {
        return a + b;
    }

    @Override
    public int sub(int a, int b) {
        return a - b;
    }

    @Override
    public int div(int a, int b) {
        return a / b;
    }

    @Override
    public int mut(int a, int b) {
        return a * b;
    }
}
第三步:切面 注解实现
@Component  //为切面加入Spring自动扫描支持
@Aspect  //加入AspectJ支持,声明类为切面
public class LogAspect {
    //前置增强
    @Before("execution(public int com.hc.Calculator.*(int,int))")
    public void beforeMethod() {
        System.out.println("前置增强");
    }

    //后置增强
    @After("execution(public int com.hc.Calculator.*(int,int))")
    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.print(methodName + " " + Arrays.toString(args));
        System.out.println("   后置增强");
    }

    //返回增强
    @AfterReturning(value = "execution(public int com.hc.Calculator.*(int,int))",
            returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint, int result) {
        String methodName = joinPoint.getSignature().getName(); //获取方法名
        Object[] args = joinPoint.getArgs(); //获取参数列表
        System.out.print(methodName + " " + Arrays.toString(args) + " " + result);
        System.out.println("   返回增强");
    }

    //异常增强
    @AfterThrowing(value = "execution(public int com.hc.Calculator.*(int,int))",
            throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint, ArithmeticException ex) { //---①
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.print(methodName + " " + Arrays.toString(args) + " " + ex);
        System.out.println("   异常增强");
    }

    //环绕增强
    @Around(value = "execution(public int com.hc.Calculator.*(int,int))")
    public Object AroundMethod(ProceedingJoinPoint pjp) {
        Object res = null;
        try {
            System.out.println("环绕增强--前置");
            res = pjp.proceed();//调用目标方法
            System.out.println("环绕增强--返回" + res);
        } catch (Throwable throwable) {
            System.out.println("环绕增强--异常");
            throw new RuntimeException(throwable);  //-----③
        }
        System.out.println("环绕增强--后置");
        return res;
    }
}

切面可以简写为:

@Component  //为切面加入Spring自动扫描支持
@Aspect  //加入AspectJ支持,声明类为切面
public class LogAspect {
    //切入点
    @Pointcut("execution(public int com.hc.Calculator.*(int,int))")
    public void logPointcut() {
    }
    //前置增强
    @Before("logPointcut()")
    public void beforeMethod() {
        System.out.println("前置增强");
    }
    //后置增强
    @After("logPointcut()")
    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.print(methodName + " " + Arrays.toString(args));
        System.out.println("   后置增强");
    }
    //返回增强
    @AfterReturning(pointcut = "logPointcut()", returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint, int result) {
        String methodName = joinPoint.getSignature().getName(); //获取方法名
        Object[] args = joinPoint.getArgs(); //获取参数列表
        System.out.print(methodName + " " + Arrays.toString(args) + " " + result);
        System.out.println("   返回增强");
    }
    //异常增强
    @AfterThrowing(pointcut = "logPointcut()", throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint, ArithmeticException ex) { //---①
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.print(methodName + " " + Arrays.toString(args) + " " + ex);
        System.out.println("   异常增强");
    }
    //环绕增强
    @Around("logPointcut()")
    public Object AroundMethod(ProceedingJoinPoint pjp) {
        Object res = null;
        String methodName = pjp.getSignature().getName();
        Object[] args = pjp.getArgs();
        try {
            System.out.println("环绕增强--前置");
            res = pjp.proceed();//调用目标方法
            System.out.println("环绕增强--返回" + res);
        } catch (Throwable throwable) {
            System.out.println("环绕增强--异常");
            throw new RuntimeException(throwable);  //-----③
        }
        System.out.println("环绕增强--后置");
        return res;
    }
}
XML实现
  • 切面类
public class LogAspect {
    public void beforeMethod() {
        System.out.println("前置增强");
    }

    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.print(methodName + " " + Arrays.toString(args));
        System.out.println("   后置增强");
    }

    public void afterReturningMethod(JoinPoint joinPoint, int result234) {
        String methodName = joinPoint.getSignature().getName(); //获取方法名
        Object[] args = joinPoint.getArgs(); //获取参数列表
        System.out.print(methodName + " " + Arrays.toString(args) + " " + result234);
        System.out.println("   返回增强");
    }


    public void afterThrowingMethod(JoinPoint joinPoint, ArithmeticException ex) { //---①
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.print(methodName + " " + Arrays.toString(args) + " " + ex);
        System.out.println("   异常增强");
    }


    public Object AroundMethod(ProceedingJoinPoint pjp) {
        Object res = null;
        String methodName = pjp.getSignature().getName();
        Object[] args = pjp.getArgs();
        try {
            System.out.println("环绕增强--前置");
            res = pjp.proceed();//调用目标方法
            System.out.println("环绕增强--返回" + res);
        } catch (Throwable throwable) {
            System.out.println("环绕增强--异常");
            throw new RuntimeException(throwable);  //-----③
        }
        System.out.println("环绕增强--后置");
        return res;
    }
}
  • xml配置


    
    

    
        
        
            
            
            
            
            
        
    


第四步:测试
@ExtendWith(SpringExtension.class)
@ContextConfiguration("/applicationContext.xml")
public class AOPTest {
    @Resource
    private Calculator calculator;

    @Test
    void fun1(){
        int addRes = calculator.add(3, 5);
        System.out.println(addRes);
    }

    @Test
    void fun2(){
        int divRes = calculator.div(3,0);
        System.out.println(divRes);
    }
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0445s