zoukankan      html  css  js  c++  java
  • Springboot学习09:AOP

    Springboot学习09:AOP

     基础概念图

    源码示例

    切点

    import org.springframework.web.bind.annotation.*;
    
    @RestController
    public class AopController {
    
        @GetMapping("/beforeAop")
        @AopBeforeAnno
        public String beforeAop(){
            System.out.println("beforeAop controller....");
            return "Success";
        }
    
        @GetMapping("/afterAop")
        @AopAfterAnno
        public String afterAop(){
            System.out.println("afterAop controller....");
            return "Success";
        }
        @PostMapping("/aroundAop/{id}")
        @AopAroundAnno
        public String aroundAop(
                @PathVariable Long id,
                @RequestParam String code,
                @RequestBody AroundAopReq aroundAopReq){
            System.out.println("aroundAop controller....");
            return "Success";
        }
    }

    切面

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    import java.util.Arrays;
    
    @Component
    @Aspect
    public class AopHandler {
    
        //1-前置通知
        //@Pointcut("execution(* com.example.bigdemo.aop.AopController.*(..))")
        @Pointcut("@annotation(com.example.bigdemo.aop.AopBeforeAnno)")
        public Object beforePointCut(){
    
            return null;
        }
    
    
        @Before(value="beforePointCut()")
        public Object before(){
            System.out.println("start aop before task here...");
            return null;
        }
    
        //后置通知
        @Pointcut("@annotation(com.example.bigdemo.aop.AopAfterAnno)")
        public Object afterPointCut(){
            return null;
        }
    
        @After("afterPointCut()")
        public Object after(){
            System.out.println("start aop After task here...");
            return null;
        }
    
        //环绕通知
        @Pointcut("@annotation(com.example.bigdemo.aop.AopAroundAnno)")
        public Object aroundPointCut(){
    
            return null;
        }
    
        @Around("aroundPointCut()")
        public Object around( ProceedingJoinPoint proceedingJoinPoint){
            System.out.println("start aop around task here...");
            try {
                System.out.println(Arrays.stream(proceedingJoinPoint.getArgs()));
                Arrays.stream(proceedingJoinPoint.getArgs()).forEach(args->{
                    System.out.println(args);
                });
    
                System.out.println(proceedingJoinPoint.getKind());
                System.out.println(proceedingJoinPoint.getSignature());
                System.out.println(proceedingJoinPoint.getTarget());
                System.out.println(proceedingJoinPoint.getThis());
                proceedingJoinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            System.out.println("end aop around task here...");
            return null;
        }
    
    
    }

    自定义注解和POJO

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface AopAfterAnno {
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface AopAroundAnno {
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface AopBeforeAnno {
    }
    
    
    public class AroundAopReq {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    END

  • 相关阅读:
    基础知识梳理
    计算机基础
    IAR平台下使用STM32的DSP配置方法
    第五节:STM32输入捕获(用CubeMX学习STM32)
    第四节:定时器中断及定时器产生PWM(用CubeMX学习STM32)
    第三节: 串口通信(用CubeMX学习STM32)
    第二节: 外部中断学习(用CubeMX学习STM32)
    第一节补充: 按键操作(CubeMX加HAL库学STM32系列)
    第一节:用Cube学32之简单IO口操作(点灯及按键)
    STM32程序中使用printf打印中文字符乱码
  • 原文地址:https://www.cnblogs.com/wobuchifanqie/p/12829588.html
Copyright © 2011-2022 走看看