美文网首页
手把手教你springboot 使用AOP切面编程及详细解析

手把手教你springboot 使用AOP切面编程及详细解析

作者: yufw | 来源:发表于2020-04-27 23:38 被阅读0次

    手把手教你springboot 使用AOP切面编程

    注解

    关于Aspect 注解的详细解释

    @Aspect:作用是把当前类标识为一个切面供容器读取

    @Pointcut:Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,二是方法签名。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为 此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码
    @Around:环绕增强,相当于MethodInterceptor
    @AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行
    @Before:标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有
    @AfterThrowing:异常抛出增强,相当于ThrowsAdvice
    @After: final增强,不管是抛出异常或者正常退出都会执行

    执行顺序

    @Around前>@Before > @Pointcut> @Around后>@After>@AfterReturning/@AfterThrowing

    springboot-maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    

    测试源码

    package com.studynote.AOP;
    
    import com.studynote.annotion.MyAnnotation;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    
    @Aspect
    @Component
    public class MyAspect {
    
        // 注解声明切点,注解的全限定名
        @Pointcut("@annotation(com.studynote.annotion.MyAnnotation)")
        public void annotationPointcut() {
        };
    
        @Before("annotationPointcut()")
        public void doBefore(JoinPoint joinPoint){
            System.out.println("前事件=========================================================");
            getAnnotation(joinPoint);
        }
    
        @After("annotationPointcut()")
        public void after(JoinPoint joinPoint) {
            System.out.println("后事件========================================================");
    
        }
    
        @AfterReturning("annotationPointcut()")
        public void afterReturning(JoinPoint joinPoint) {
            System.out.println("@AfterReturning========================================================");
    
        }
        @AfterThrowing("annotationPointcut()")
        public void afterThrowing(JoinPoint joinPoint) {
            System.out.println("@AfterThrowing========================================================");
    
        }
    
        @Around("annotationPointcut()")
        public Object process(ProceedingJoinPoint point) throws Throwable {
            System.out.println("@Around:执行目标方法之前...");
            //访问目标方法的参数: 可以修改入参
            Object[] args = point.getArgs();
            if (args != null && args.length > 0 && args[0].getClass() == String.class) {
                args[0] = "修改参数";
            }
            Object returnValue = point.proceed(args);
            System.out.println("@Around:执行目标方法之后...");
            System.out.println("@Around:target:" + point.getTarget());
            return "原返回值:" + returnValue + ",这是返回结果的后缀";
        }
    
        private void getAnnotation(JoinPoint joinPoint) {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            //获取执行方法名
            String methodName = method.getName();
            MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
            if(null !=  myAnnotation){
                //获取注解参数值
                System.out.println("===========获取注解参数值==========");
                System.out.println("value: "+myAnnotation.value());
            }
            //获取方法传递的参数
            Object[] args = joinPoint.getArgs();
            if(null != args){
                System.out.println("===========获取被注解方法的 入参==========");
                for(Object o : args){
                    System.out.println("方法传递的参数: "+ o.toString());
                }
            }
            System.out.println("==============获取被注解方法的方法名"+methodName+"=============");
        }
    
    
    }
    
    

    测试结果:

    • 抛异常
    @Around:执行目标方法之前...
    前事件=========================================================
    ===========获取注解参数值==========
    value: 注解value属性值
    ===========获取被注解方法的 入参==========
    方法传递的参数: 修改参数
    ==============获取被注解方法的方法名aoptest=============
    ========================
    后事件========================================================
    @AfterThrowing========================================================
    
    • 正常运行

    • @Around:执行目标方法之前...
      前事件=========================================================
      ===========获取注解参数值==========
      value: 注解value属性值
      ===========获取被注解方法的 入参==========
      方法传递的参数: 修改参数
      ==============获取被注解方法的方法名aoptest=============
      ========================
      @Around:执行目标方法之后...
      @Around:target:com.studynote.AOP.AOPTest@2d3dfffa
      后事件========================================================
      @AfterReturning========================================================
      

    源码地址:https://gitee.com/twelfthLunarMonthFourteen/pub_beauty/tree/hotfix/study-note/src/main/java/com/studynote/AOP

    个人水平有限,如有问题,请各路大神指教,虚心接纳

    如果觉得有帮助,请点赞收藏,谢谢

    相关文章

      网友评论

          本文标题:手把手教你springboot 使用AOP切面编程及详细解析

          本文链接:https://www.haomeiwen.com/subject/zfzpwhtx.html