• Day 93


    _Spring技术–AOP通知获取数据

    1. 获取切入点方法的参数:
      • JoinPoint:适用于前置、后置、返回后、抛出异常后通知
      • ProceedJoint:适用于环绕通知
    2. 获取切入点方法返回值
      • 返回后通知
      • 环绕通知
    3. 获取切入点方法运行异常信息
      • 抛出异常通知
      • 环绕通知

    _Spring技术–AOP通知获取数据–@Before & @After

    1. 如图所示,在业务层的实现类BookService中有两个方法:runBefore() 和 runAfter() :在这里插入图片描述

    2. 然后再aop包中配置AOP的增强程序:

      • package com.Alvis.aop;
        
        import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.*;
        import org.springframework.stereotype.Component;
        
        import java.util.Arrays;
        
        @Component
        @Aspect
        public class MyAdvice {
            // 描述切面方法
            @Pointcut("execution(* com.Alvis.service.BookService.*(..))")
            public void servicePt() {}
        
        
        //    @Before("MyAdvice.servicePt()")
            public void before(JoinPoint joinPoint) {
                Object[] args = joinPoint.getArgs();
                System.out.println("before ...It has been executed");
                System.out.println(Arrays.toString(args));
            }
        
        //    @After("MyAdvice.servicePt()")
            public void after(JoinPoint joinPoint) {
                Object[] args = joinPoint.getArgs();
                System.out.println("After ...It has been executed");
                System.out.println(Arrays.toString(args));
            }
        
        
            @Around("MyAdvice.servicePt()")
            public Object around(ProceedingJoinPoint pjp) throws Throwable {
                Object[] args = pjp.getArgs();
                System.out.println("Around ...It has been executed");
        
                // 可以在此修改源方法传入的参数值
                args[0] = 1000;
                System.out.println(Arrays.toString(args));
                Object ret = pjp.proceed(args);
                return ret;
            }
        }
        
        • 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
      • 在这里插入图片描述

      • 在test文件中编写BookServiceTest类测试方法:

        • package com.Alvis.service;
          
          import com.Alvis.config.SpringConfig;
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.annotation.AnnotationConfigApplicationContext;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
          
          
          // 设定类运行器
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(classes = SpringConfig.class)
          
          
          public class ServiceTest {
              private BookService bookService;
          
              @Test
              public void testBookServiceBefore() {
                  ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
                  BookService bookService = apx.getBean(BookService.class);
                  bookService.runBefore(90);
              }
          
              @Test
              public void testBookServiceAfter() {
                  ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
                  BookService bookService = apx.getBean(BookService.class);
                  bookService.runAfter(100);
              }
          
              @Test
              public void testBookServiceAround() {
                  ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
                  BookService bookService = apx.getBean(BookService.class);
                  bookService.runAround(0 , "张三");
              }
          }
          ================================================
          before ...It has been executed
          [90]
          this is runBefore...
          age is ...90
          
          进程已结束,退出代码0
          ================================================
          
          this is runAfter
          age is ...100
          After ...It has been executed
          [100]
          
          进程已结束,退出代码0
          
          
          • 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

    _Spring技术–AOP–通知获取数据–@Around

    1. @Around可以对源方法传入的参数进行调整

    2. 在aop包中的MyAdvice类中的@Around方法描述如图所示:在这里插入图片描述

    3. 最后在test文件的BookServiceTest类中对AOP@Around通知修改源方法的参数值作测试

      • 源方法中传入的参数值如图所示:在这里插入图片描述

      •     @Test
            public void testBookServiceAround() {
                ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
                BookService bookService = apx.getBean(BookService.class);
                bookService.runAround(0 , "张三");
            }
        =======================================
        Around ...It has been executed
        [1000, 张三]
        this is runAround
        name = 张三  age is ...1000
        
        进程已结束,退出代码0
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
      • 在这里插入图片描述

  • 相关阅读:
    云计算网络信息安全防护思路探究
    flink操作hudi数据表
    【学习】软件压力测试对软件产品的作用
    十年架构五年生活-09 五年之约如期而至
    JAVA--枚举类
    STM32 float浮点数转换成四个字节
    数据加密和BCrypt哈希算法应用 | StartDT Tech Lab 15
    vmware使用桥接模式无法ping通本地和外网的解决方法
    智能小车开发篇 - 低时延直播测试
    u盘打不开,提示需要格式化怎么办?
  • 原文地址:https://blog.csdn.net/ALVIS_108/article/details/126572307