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

  • 相关阅读:
    WCF 第十二章 对等网 实现一个自定义对等网解析器
    WCF 第十二章 对等网 System.Net.PeerToPeer.Collaboration
    WCF 第十二章 对等网 使用Windows Vista 来进行合作
    WCF 第十二章 对等网 使用PNRP解决对等网络问题
    WCF 第十二章 对等网 点对点应用程序
    WCF 第十二章 对等网 限制一条消息的跳数
    WCF 第十二章 对等网 创建P2P应用程序
    C#解析HTML
    C#中的DLL注入
    VC简单实现淡入淡出效果
  • 原文地址:https://www.cnblogs.com/wobuchifanqie/p/12829588.html
Copyright © 2011-2022 走看看