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

手把手教你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

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

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring的AOPAOP的基本概念基于注解的“零配置”方式定义切面Bean定义Before增强处理定义After...
    渐丶忘阅读 1,688评论 0 0
  • AOP实现可分为两类(按AOP框架修改源代码的时机): 静态AOP实现:AOP框架在编译阶段对程序进行修改,即实现...
    数独题阅读 2,422评论 0 22
  • 一、简述 1、AOP的概念 如果你用java做过后台开发,那么你一定知道AOP这个概念。如果不知道也无妨,套用百度...
    GitLqr阅读 4,431评论 6 25
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 3,483评论 0 9
  • 【spring-boot】spring aop 面向切面编程初接触 众所周知,spring最核心的两个功能是aop...
    可可西里的星星阅读 530评论 1 0

友情链接更多精彩内容