SpringAOP 如何通过JoinPoint获取参数名和值

作者:alwaysBrother 时间:2023-11-01 00:50:36 

SpringAOP 通过JoinPoint获取参数名和值

在Java8之前,代码编译为class文件后,方法参数的类型固定,但是方法名称会丢失,方法名称会变成arg0、arg1….。在Java8开始可以在class文件中保留参数名。


public void tet(JoinPoint joinPoint) {
       // 下面两个数组中,参数值和参数名的个数和位置是一一对应的。
       Object[] args = joinPoint.getArgs(); // 参数值
       String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames(); // 参数名
}

注意:

IDEA 只有设置了 Java 编译参数才能获取到参数信息。并且jdk要在1.8及以上版本。

SpringAOP 如何通过JoinPoint获取参数名和值

Maven中开启的办法

增加compilerArgs 参数


<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven_compiler_plugin_version}</version>
        <configuration>
            <source>${java_source_version}</source>
            <target>${java_target_version}</target>
            <encoding>${file_encoding}</encoding>
            <compilerArgs>
                <arg>-parameters</arg>
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>

Eclipse中开启的办法

Preferences->java->Compiler下勾选Store information about method parameters选项。

这样在使用eclipse编译java文件的时候就会将参数名称编译到class文件中。

SpringAOP中JoinPoint对象的使用方法

JoinPoint 对象

JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.

常用API

方法名功能
Signature getSignature();获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
Object[] getArgs();获取传入目标方法的参数对象
Object getTarget();获取被代理的对象
Object getThis();获取代理对象

ProceedingJoinPoint对象

ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中,

添加了以下两个方法。


Object proceed() throws Throwable //执行目标方法
Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法

Demo

切面类


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

被代理类


/**
* 被代理对象
*/
@Component
public class TargetClass {
   /**
    * 拼接两个字符串
    */
   public String joint(String str1, String str2) {
       return str1 + "+" + str2;
   }
}

测试类


public class TestAop {
   @Test
   public void testAOP() {
       //1、创建Spring的IOC的容器
       ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");
       //2、从IOC容器中获取bean的实例
       TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");
       //3、使用bean
       String result = targetClass.joint("spring","aop");
       System.out.println("result:" + result);
   }
}

输出结果

目标方法执行前...
目标方法名为:joint
目标方法所属类的简单类名:TargetClass
目标方法所属类的类名:aopdemo.TargetClass
目标方法声明类型:public
第1个参数为:newSpring
第2个参数为:newAop
被代理的对象:aopdemo.TargetClass@4efc180e
代理对象自己:aopdemo.TargetClass@4efc180e (和上面一样是因为toString方法也被代理了)
目标方法返回结果后...
目标方法执行后...
result:newSpring+newAop

来源:https://blog.csdn.net/u013041642/article/details/81209404

标签:SpringAOP,JoinPoint,参数名,参数值
0
投稿

猜你喜欢

  • java读写二进制文件的解决方法

    2022-08-03 14:45:55
  • Java事务的个人理解小结

    2023-11-29 12:10:37
  • 时间处理函数工具分享(时间戳计算)

    2021-07-24 05:06:18
  • Java实战之基于swing的QQ邮件收发功能实现

    2023-11-15 01:34:26
  • Java与C++分别用递归实现汉诺塔详解

    2021-10-23 01:28:59
  • Java框架---Spring详解

    2021-07-09 14:27:30
  • Java基于Socket实现网络编程实例详解

    2023-11-23 12:22:37
  • Java中SimpleDateFormat日期格式转换详解及代码示例

    2023-09-04 22:13:43
  • spring-cloud-gateway动态路由的实现方法

    2021-07-25 15:24:37
  • Java 用Prometheus搭建实时监控系统过程详解

    2023-09-06 12:07:40
  • java常用工具类 Random随机数、MD5加密工具类

    2023-02-14 17:55:08
  • Spring Boot使用Allatori代码混淆的方法

    2023-11-24 16:34:55
  • 浅谈Java三目运算

    2023-11-29 07:27:59
  • 简单阐述一下Java集合的概要

    2023-08-23 19:49:45
  • maven之packaging标签的使用

    2021-10-25 05:14:42
  • Java 字符串转float运算 float转字符串的方法

    2022-04-09 10:09:06
  • Java选择排序和垃圾回收机制详情

    2023-10-23 16:53:38
  • SpringBoot上传文件大小受限问题的解决办法

    2023-04-19 09:46:16
  • 分析并发编程之LongAdder原理

    2023-05-11 17:19:30
  • SpringBoot过滤器与拦截 器深入分析实现方法

    2023-11-28 23:04:15
  • asp之家 软件编程 m.aspxhome.com