Spring如何在没有调试信息的情况下获取参数名称 [英] How Spring gets parameter names without debug information

查看:140
本文介绍了Spring如何在没有调试信息的情况下获取参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@RequestMapping("/form")
public String form(Model model, Integer id)

例如,spring可以知道参数id的名称为id并将请求参数的值绑定到运行时

For example ,spring can know parameter id's name is id and bind request param's value to it in runtime

推荐答案

这是JDK 8中引入的javac的功能.您需要包括-parameters javac命令行选项以将其激活.然后,您将能够获得像这样的参数名称:

This is a feature of javac introduced in JDK 8. You need to include -parameters javac command line option to activate it. Then you will be able to get parameter names like this:

String name = String.class.getMethod("substring", int.class).getParameters()[0].getName()
System.out.println(name);

摘自Spring文档 3.3 Java 8(以及6和7)

From Spring documentation 3.3 Java 8 (as well as 6 and 7)

您还可以使用Java 8的参数名称发现(基于 -parameters编译器标志)作为在启用调试信息的情况下编译代码的替代方法.

You can also use Java 8’s parameter name discovery (based on the -parameters compiler flag) as an alternative to compiling your code with debug information enabled.

在Javadoc中为 ParameterNameDiscoverer 类:

As specified in the javadoc for ParameterNameDiscoverer class:

并非总是可以找到参数名称,但是可以发现 可以尝试一些策略,例如查找调试信息 可能是在编译时发出的,并寻找argname 批注值(可选地附带AspectJ批注的方法).

Parameter name discovery is not always possible, but various strategies are available to try, such as looking for debug information that may have been emitted at compile time, and looking for argname annotation values optionally accompanying AspectJ annotated methods.

这是Spring项目方法和构造函数的Java 8参数名称发现中指向相关JIRA项目的链接

This is the link to related JIRA item in Spring project Java 8 parameter name discovery for methods and constructors

JDK增强建议 JEP 118:在运行时访问参数名称

这篇关于Spring如何在没有调试信息的情况下获取参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆