• AOP学习


    package com.zyp.aspect;
    
    import com.zyp.common.NoLogin;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import java.lang.reflect.Method;
    
    /**
     * 通知的执行顺序  环绕通知(around)-->前置通知(before)--->
     * 成功返回通知(afterReturning)/失败通知(afterThrowing)--->后置通知(after)--->环绕通知
     * @author syl
     * @description TODO
     * @since 2022/6/23
     */
    //定义一个切面
    @Aspect
    //交给springr容器管理
    @Component
    //多个切面时,定义切面的执行顺序,数字越小顺序越靠前
    @Order(2)
    @Slf4j
    public class AopTest {
    
        /**
         * 定义切入点
         */
        @Pointcut("execution(public * com.zyp.controller..*.*(..))")
        public void pointCut(){}
    
    
        @Before("pointCut()")
        public void before(){
            log.info("前置通知");
        }
    
        @After("pointCut()")
        public void after(){
            log.info("后置通知");
        }
    
        /**
         * ProceedingJoinPoint是环绕通知特有的对象
         * @param joinPoint
         * @return
         * @throws Throwable
         */
        @Around("pointCut()")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            log.info("环绕通知");
            //获取目标方法参数
            Object[] args = joinPoint.getArgs();
            //获取目标方法的方法签名
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            //获取目标方法的方法对象
            Method method = methodSignature.getMethod();
            NoLogin annotation = method.getAnnotation(NoLogin.class);
            if (annotation != null) {
                log.info("免密登录");
            }else{
                //获取请求上下文
                ServletRequestAttributes servletRequestAttributes =
                        (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = servletRequestAttributes.getRequest();
                request.getHeader("token");
            }
            //获取目标方法的返回值对象
            Class returnType = methodSignature.getReturnType();
            //执行目标方法并接收返回结果
            Object result = joinPoint.proceed();
            log.info("环绕通知");
            return result;
        }
    
        @AfterReturning("pointCut()")
        public void afterReturning(JoinPoint joinPoint) throws Throwable {
            log.info("成功返回通知");
        }
    
        @AfterThrowing("pointCut()")
        public void afterThrowing(JoinPoint joinPoint){
            log.info("异常通知");
        }
    }
    
    • 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
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
  • 相关阅读:
    开源交流丨任务or实例 详解大数据DAG调度系统Taier任务调度
    Revit中“结构框架显示与剪切“的应用和一键剪切功能
    ScanNet数据集下载与导出颜色图、深度图、内参、位姿数据
    vuepress-plugin-comment -use Valine
    C# WPF: Imag图片填充方式有哪些?
    opencv-python之图像的加法与按位运算
    AOP是什么?如何使用AOP?
    有向图计数优化版原理及C++实现
    http相应状态码,重定向和转发区别,文件下载流对应格式
    世界65个国家贸易开放度数据 2005-2019年
  • 原文地址:https://blog.csdn.net/m0_46360888/article/details/125511092