栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

第一步:添加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);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/933675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号