SpringAOP中通过JoinPoint获取参数名和值

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u013041642/article/details/81209404

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及以上版本。
这里写图片描述

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文件中。

猜你喜欢

转载自blog.csdn.net/u013041642/article/details/81209404