当前位置: 代码网 > it编程>编程语言>Java > SpringBoot AOP中JoinPoint的使用方式和通知切点表达式

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

2024年05月26日 Java 我要评论
joinpoint和proceedingjoinpoint对象joinpoint对象封装了springaop中切面方法的信息,在切面方法中添加joinpoint参数,就可以获取到封装了该方法信息的jo

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;        
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com