Spring AOP(1) --基于注解方式配置

1、spring-aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <!--扫描aop包下面的注解-->
    <context:component-scan base-package="com.spring.aop"/>

    <!--使Aspect注解生效,自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy/>
</beans>

2、LoggingAspect.java

  1 package com.spring.aop.aspect;
  2 
  3 import org.aspectj.lang.JoinPoint;
  4 import org.aspectj.lang.ProceedingJoinPoint;
  5 import org.aspectj.lang.annotation.*;
  6 import org.springframework.core.annotation.Order;
  7 import org.springframework.stereotype.Component;
  8 
  9 import java.util.Arrays;
 10 import java.util.List;
 11 
 12 /**
 13  * @author liangd
 14  * date 2020-11-16 17:40
 15  * code 把这个类声明为一个切面
 16  * 1、加入到IOC容器中
 17  * 2、声明为一个切面
 18  */
 19 @Order(2)
 20 @Aspect
 21 @Component
 22 public class LoggingAspect {
 23     /**
 24      * 声明切入点表达式,重用代码
 25      * 使用@PointCut来声明表达式
 26      * 后面其它通知直接应该该方法
 27      */
 28     @Pointcut("execution(* com.spring.aop.aspect.MathCalculator.*(..))")
 29     public void declareJoinPointExpress(){}
 30 
 31     /**
 32      * 前置通知:在执行方法前执行
 33      * execution:执行需要需要加入切面的方法
 34      * execution(public int com.spring.aop.aspect.MathCalculator.add(int ,int)) 此路径只对add方法有效
 35      * execution(* com.spring.aop.aspect.MathCalculator.*(int ,int)) 表示将该类下的任意修饰符、任意返回值和
 36      * 带(int,int)参数的方法加入到切面中
 37      */
 38 //    @Before("execution(public int com.spring.aop.aspect.MathCalculator.*(int ,int))")
 39     @Before("declareJoinPointExpress()")
 40     public void beforeMethod(JoinPoint joinPoint) {
 41         //获取切入点方法名
 42         String methodName = joinPoint.getSignature().getName();
 43         //获取切入点方法参数
 44         List<Object> args = Arrays.asList(joinPoint.getArgs());
 45 
 46         System.out.println("The method " + methodName + " begins with " + args);
 47     }
 48 
 49     /**
 50      * 后置通知:在目标方法之后执行(无论是否发生异常),执行的通知
 51      * 不能访问目标方法执行的结果
 52      */
 53 //    @After("execution(* com.spring.aop.aspect.MathCalculator.*(int,int))")
 54     @After("declareJoinPointExpress()")
 55     public void afterMethod(JoinPoint joinPoint) {
 56         //获取切入点方法名
 57         String methodName = joinPoint.getSignature().getName();
 58         System.out.println("The method " + methodName + " ends with");
 59     }
 60 
 61     /**
 62      * 返回通知:可以访问目标方法执行的结果
 63      *
 64      * @param joinPoint 切入点
 65      * @param result    返回结果
 66      */
 67     @AfterReturning(value = "declareJoinPointExpress()",
 68             returning = "result")
 69     public void afterReturning(JoinPoint joinPoint, Object result) {
 70 
 71         String methodName = joinPoint.getSignature().getName();
 72 
 73         System.out.println("The method " + methodName + " returning with " + result);
 74     }
 75 
 76     /**
 77      * 异常通知:在方法抛出异常的时候执行
 78      */
 79     @AfterThrowing(value = "declareJoinPointExpress()",
 80             throwing = "e")
 81     public void afterThrowing(JoinPoint joinPoint, Exception e) {
 82         String methodName = joinPoint.getSignature().getName();
 83 
 84         System.out.println("The method " + methodName + " occurs throwing with " + e);
 85     }
 86 
 87     /**
 88      * 环绕通知:相当于动态代理的全过程(功能最强大,但并不代表经常使用)
 89      * <p>
 90      * 环绕通知需要ProceedingJoinPoint 参数,参数的类型可以决定是否执行目标方法
 91      * 环绕通知必须有返回值,且返回值为目标方法的返回值
 92      *
 93      * @param pjp 参数类型
 94      * @return object
 95      */
 96   /*  @Around(value = "execution(* com.spring.aop.aspect.MathCalculator.*(..))")
 97     public Object aroundMethod(ProceedingJoinPoint pjp) {
 98 
 99         Object result = null;
100         String methodName = pjp.getSignature().getName();
101 
102         try {
103             //前置通知
104             System.out.println("The method (around) " + methodName + " begins with " + Arrays.asList(pjp.getArgs()));
105             result = pjp.proceed();
106             //返回通知
107             System.out.println("The method (around) " + methodName + " returning with " + result);
108         } catch (Throwable throwable) {
109             //异常通知
110             System.out.println("The method (around) " + methodName + " occurs throwing with " + throwable);
111         }
112         //后置通知
113         System.out.println("The method (around) " + methodName + " ends with");
114         return result;
115     }*/
116 }
作者:donleo123
本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
原文地址:https://www.cnblogs.com/donleo123/p/14069601.html