• 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

  • 相关阅读:
    纯代码frame位移和伸缩
    纯代码-QQ登陆练习
    IOS学习笔记 -- 基础
    LoadRunner接口脚本web_submit_data编写过程中遇到的问题及分享
    Win10系统下安装Oracle服务器和Oracle客户端
    Liunx上传下载和压缩问题分享
    LoadRunner接口工作总结
    分享网上搜到的Oracle中对判定条件where 1=1的正解
    LoadRunner接口测试Error -27225报错解决
    LoadRunner压力测试之Unique Number参数类型、Random Number参数类型浅析
  • 原文地址:https://www.cnblogs.com/wobuchifanqie/p/12829588.html
Copyright © 2020-2023  润新知