SpringBoot AOP中JoinPoint的使用方式和通知切点表达式

 更新时间:2024年05月13日 09:16:40   作者:程序员三时  
这篇文章主要介绍了SpringBoot AOP中JoinPoint的使用方式和通知切点表达式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

JoinPoint和ProceedingJoinPoint对象

  • JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.
  • ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中
@Aspect
@Component
public class aopAspect {
    /**
     * 定义一个切入点表达式,用来确定哪些类需要代理
     * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理
     */
    @Pointcut("execution(* aopdemo.*.*(..))")
    public void declareJoinPointerExpression() {}

    /**
     * 前置方法,在目标方法执行前执行
     * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
     */
    @Before("declareJoinPointerExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
        System.out.println("目标方法所属类的简单类名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "个参数为:" + args[i]);
        }
        System.out.println("被代理的对象:" + joinPoint.getTarget());
        System.out.println("代理对象自己:" + joinPoint.getThis());
    }

    /**
     * 环绕方法,可自定义目标方法执行的时机
     * @param pjd JoinPoint的子接口,添加了
     *            Object proceed() throws Throwable 执行目标方法
     *            Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法
     *            两个方法
     * @return 此方法需要返回值,返回值视为目标方法的返回值
     */
    @Around("declareJoinPointerExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        Object result = null;

        try {
            //前置通知
            System.out.println("目标方法执行前...");
            //执行目标方法
            //result = pjd.proeed();
            //用新的参数值执行目标方法
            result = pjd.proceed(new Object[]{"newSpring","newAop"});
            //返回通知
            System.out.println("目标方法返回结果后...");
        } catch (Throwable e) {
            //异常通知
            System.out.println("执行目标方法异常后...");
            throw new RuntimeException(e);
        }
        //后置通知
        System.out.println("目标方法执行后...");

        return result;
    }
}

切点表达式

  • 在Spring AOP中,连接点始终代表方法的执行。切入点是与连接点匹配的,切入点表达语言是以编程方式描述切入点的方式。
  • 切入点(Poincut)是定义了在“什么地方”进行切入,哪些连接点会得到通知。显然,切点一定是连接点
  • 切点是通过@Pointcut注解和切点表达式定义的。@Pointcut注解可以在一个切面内定义可重用的切点。

execute表达式

*代表匹配任意修饰符及任意返回值,参数列表中..匹配任意数量的参数

可以使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系

  • 1.拦截任意公共方法execution(public * *(..))
  • 2.拦截以set开头的任意方法execution(* set*(..))
  • 3.拦截类或者接口中的方法
拦截AccountService(类、接口)中定义的所有方法
execution(* com.xyz.service.AccountService.*(..))

4.拦截包中定义的方法,不包含子包中的方法

拦截com.xyz.service包中所有类中任意方法,**不包含**子包中的类
execution(* com.xyz.service.*.*(..))

5.拦截包或者子包中定义的方法

拦截com.xyz.service包或者子包中定义的所有方法
execution(* com.xyz.service..*.*(..))

通知分类

@Before

  • 前置通知: 在方法执行之前执行
  • 前置通知使用@Before注解 将切入点表达式值作为注解的值

@After

  • 后置通知, 在方法执行之后执行
  • 后置通知使用@After注解 ,在后置通知中,不能访问目标方法执行的结果

@AfterRunning

  • 返回通知, 在方法返回结果之后执行
  • 返回通知使用@AfterRunning注解

@AfterThrowing

  • 异常通知, 在方法抛出异常之后执行
  • 异常通知使用@AfterThrowing注解

@Around

  • 环绕通知, 围绕着方法执行
  • 环绕通知使用@Around注解

package com.jason.spring.aop.impl;
 
import java.util.Arrays;
import java.util.List;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
 
 
//把这个类声明为一个切面
//1.需要将该类放入到IOC 容器中
@Component
//2.再声明为一个切面
@Aspect
public class LoggingAspect {
    
    //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
    //作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
    //支持通配符
    //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
    @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins " + args);
    }
    
    /**
     * @Description:  在方法执行后执行的代码,无论该方法是否出现异常
     * @param joinPoint
     */
    @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " end " + args);
    }
    
    /**
     * 
     * @Description:  在方法正常结束后执行代码,放回通知是可以访问到方法的返回值
     *
     * @param joinPoint
     */
    @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")
    public void afterReturning(JoinPoint joinPoint ,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " end with " + result);
    }
    
    /**
     * 
     * @Description:  在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码
     *
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")
    public void afterThrowting(JoinPoint joinPoint, Exception  ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs exceptions " + ex);
    }
    
    /**
     * 
     * @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数
     *                    环绕通知 类似于  动态代理的全过程
     *                   ProceedingJoinPoint:可以决定是否执行目标方法
     *    环绕通知必须有返回值,返回值即为目标方法的返回值
     *    
     * @param proceedingJoinPoint
     */
    @Around("execution(* com.jason.spring.aop.impl.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        
        //执行目标方法
        try {
            //前置通知
            System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));
            
            result = proceedingJoinPoint.proceed();
            
            //后置通知
            System.out.println("The method " + methodName + "end with" + result);
            
        } catch (Throwable e) {
            //异常通知
            System.out.println("The method occurs exception : " + e);
            throw new RuntimeException();
        }
            //后置通知
            
        System.out.println("The method " + methodName + "end with" + result);
        
        return result;        
    }
}

切点表达式参考

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详谈Array和ArrayList的区别与联系

    详谈Array和ArrayList的区别与联系

    下面小编就为大家带来一篇详谈Array和ArrayList的区别与联系。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • springmvc使用REST出现:Request method 'PUT' not supported问题

    springmvc使用REST出现:Request method 'PUT' not sup

    这篇文章主要介绍了springmvc使用REST出现:Request method 'PUT' not supported问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • SpringBoot Security+JWT简单搭建的实现示例

    SpringBoot Security+JWT简单搭建的实现示例

    本文介绍在Spring Boot 2.6.13项目中集成Security与JWT实现认证鉴权的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-09-09
  • Java基础之Integer使用的注意事项及面试题

    Java基础之Integer使用的注意事项及面试题

    这篇文章主要给大家介绍了关于Java基础之Integer使用注意事项及面试题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • 使用SpringEvent解决WebUploader大文件上传解耦问题

    使用SpringEvent解决WebUploader大文件上传解耦问题

    Spring Event是Spring框架内建的一种发布/订阅模式的实现,它允许应用内部不同组件之间通过事件进行通信,本文以WebUploader大文件上传组件为例,在大文件处理的场景中使用SpringEvent的事件发布机制,灵活的扩展对文件的处理需求,需要的朋友可以参考下
    2024-07-07
  • Spring Gateway基本使用示例小结

    Spring Gateway基本使用示例小结

    Springcloud Gateway使用了Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架,具体一些特征,本文结合实例代码对Spring Gateway使用给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2023-11-11
  • Spring controller校验入参的方法详解

    Spring controller校验入参的方法详解

    项目中使用Springboot,在Controller中配置了@NotNull和@Valid,@Notnull不生效,@Valid生效,返回http status为400,本文给大家介绍了Spring controller校验入参的方法,需要的朋友可以参考下
    2024-06-06
  • java的Array,List和byte[],String相互转换的方法你了解嘛

    java的Array,List和byte[],String相互转换的方法你了解嘛

    这篇文章主要为大家详细介绍了java的Array,List和byte[],String相互转换的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • SpringBoot事务失效的八大原因及解决方案

    SpringBoot事务失效的八大原因及解决方案

    在 Spring Boot 项目开发中,声明式事务管理通过 @Transactional 注解提供了极大的便利,但许多开发者都曾遇到过事务不生效的困扰,本文将详细分析导致 Spring Boot 事务失效的八大常见情况,并提供相应的解决方案,需要的朋友可以参考下
    2025-09-09
  • Java多线程程序中synchronized修饰方法的使用实例

    Java多线程程序中synchronized修饰方法的使用实例

    synchronized关键字主要北用来进行线程同步,这里我们主要来演示Java多线程程序中synchronized修饰方法的使用实例,需要的朋友可以参考下:
    2016-06-06

最新评论