• springboot注解方式实现aop及常规方式


    本文介绍springboot实现aop的两种方式
    首先需要引入对应依赖:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.9.1</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在启动类上面加上注解

    @EnableAspectJAutoProxy
    
    • 1

    其实不加这个注解也可以,aop照样会生效,我们查看spring-boot-autoconfigure的依赖,查看spring.factories文件会发现以下配置
    在这里插入图片描述
    然后查看AopAutoConfiguration类会发现,当yml没有对应配置时,默认为true
    在这里插入图片描述下面展示常规方式实现aop的示例:

    /**
     * @Description
     * @Author maruko
     * @Date 2022/11/22 17:04
     * @Version 1.0
     */
    @Aspect
    @Component
    public class AspectTest {
    
        @Pointcut("execution(* com.zjf.demo.controller.UserController.*(..))||execution(* com.zjf.demo.controller.KafkaController.*(..))")
        public void pointExpression() {
    
        }
    
        @Before("pointExpression()")
        public void before(JoinPoint joinPoint) {
    //        System.err.println(joinPoint.toString());
    //        Object[] args = joinPoint.getArgs();
    //        for (Object arg : args) {
    //            System.err.println(arg);
    //        }
    
    //        System.err.println(joinPoint.getSignature().toLongString());
    //        System.err.println(joinPoint.getSignature().toShortString());
    //        System.err.println(joinPoint.getSignature().getName());
    //        System.err.println(joinPoint.getSignature().toString());
    //        System.err.println(joinPoint.getKind());
    //        System.err.println(joinPoint.getTarget().toString());
    //        System.err.println(joinPoint.getThis().toString());
    //        System.err.println(joinPoint.getStaticPart());
    //        System.err.println(joinPoint.getSourceLocation());
    //        System.err.println(joinPoint.toLongString());
    //        System.err.println(joinPoint.toShortString());
            System.err.println("前置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
        }
    
    //    @After("execution(* com.zjf.demo.controller.UserController.*(..))")
        public void after(JoinPoint joinPoint) {
            System.err.println("后置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
    
        }
    
    //    @AfterReturning(pointcut = "execution(* com.zjf.demo.controller.UserController.*(..))", returning = "result")
        public void afterReturning(JoinPoint joinPoint, Object result) {
            System.err.println("返回通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 返回结果为:" + result);
        }
    
    //    @AfterThrowing(value = "execution(* com.zjf.demo.controller.UserController.*(..))", throwing = "exception")
        public void afterThrowing(JoinPoint joinPoint, Exception exception) {
            System.err.println("异常通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 异常为:" + exception);
        }
    
    //    @Around("execution(* com.zjf.demo.controller.UserController.*(..))")
        public Object aroundMethod(ProceedingJoinPoint joinPoint) {
    
            String methodName = joinPoint.getSignature().getName();
    
            List<Object> args = Arrays.asList(joinPoint.getArgs());
    
            Object result = null;
            try {
                //前置通知
                System.err.println("前置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
    
                result = joinPoint.proceed();
    
                //返回通知
                System.err.println("返回通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 返回结果为:" + result);
            } catch (Throwable e) {
                // 异常通知
                System.err.println("异常通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 异常为:" + e);
                throw new RuntimeException(e);
            }
    
            //后置通知
            System.err.println("后置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
    
            return result;
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    注解方式实现如下:
    首先定义注解:

    @Target(ElementType.METHOD)   //定义注解的使用范围为方法
    @Retention(RetentionPolicy.RUNTIME )
    public @interface AopAnnotation {
    }
    
    • 1
    • 2
    • 3
    • 4

    示例如下:
    /**

    • @Description

    • @Author maruko

    • @Date 2022/11/23 9:33

    • @Version 1.0
      */
      @Aspect
      @Component
      @Order(1)
      public class AspectAnnotationTest {

      @Pointcut(“@annotation(com.zjf.demo.annotation.AopAnnotation)”)
      public void pointExpression() {

      }

      @Before(“pointExpression()”)
      public void before(JoinPoint joinPoint) {
      // System.err.println(joinPoint.toString());
      // Object[] args = joinPoint.getArgs();
      // for (Object arg : args) {
      // System.err.println(arg);
      // }

    // System.err.println(joinPoint.getSignature().toLongString());
    // System.err.println(joinPoint.getSignature().toShortString());
    // System.err.println(joinPoint.getSignature().getName());
    // System.err.println(joinPoint.getSignature().toString());
    // System.err.println(joinPoint.getKind());
    // System.err.println(joinPoint.getTarget().toString());
    // System.err.println(joinPoint.getThis().toString());
    // System.err.println(joinPoint.getStaticPart());
    // System.err.println(joinPoint.getSourceLocation());
    // System.err.println(joinPoint.toLongString());
    // System.err.println(joinPoint.toShortString());
    System.err.println(“注解方式实现,前置通知,方法名为:” + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
    }
    }

  • 相关阅读:
    UVa11887 Tetrahedrons and Spheres
    【Linux】进程探秘
    ESPHome 通过http获取设备的状态
    【无标题】
    AI概念之人工智能、机器学习和数据挖掘之间的联系与区别
    【数据库】Sql Server 2022通过临时表和游标遍历方式逻辑处理获取目标数据
    Qt Creator 创建 Qt 默认窗口程序
    unity 获取3D物体的方向数据
    python爬虫练习,爬取iview,element组件库图标名称
    computer planetary——全球生物多样性信息机构 (GBIF)
  • 原文地址:https://blog.csdn.net/qq_35705176/article/details/127994334