Springs @RequestParam 注解的内部工作原理

发布于 2024-11-27 08:24:32 字数 203 浏览 1 评论 0 原文

在 Spring 中,如果我没记错的话,以下两个语句是相同的:

@RequestParam("type") String type
@RequestParam String type

Spring 如何知道“type”的变量名称(第二个版本)。我当时的印象是 该信息已从类文件中删除,除非使用 -g 标志(包括调试信息)。

In Spring, the two following statements are, if I'm not mistaken, identical:

@RequestParam("type") String type
@RequestParam String type

How can spring know the variable name of 'type' (second version). I was under the impression
that this information was removed from the class files unless compiled with
the -g flag (include debug information).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2024-12-04 08:24:32

简而言之,显然参数名称正在被编译,如果没有,您会得到一个异常,表明 Spring MVC 无法推断出参数名称。也就是说,参数名称并不总是存储在字节码中,但似乎如果是,Spring 会找到它们,如果不是,则需要在添加 @RequestParam 注解时指定它们。

其他详细信息可参见此类似问题,它是答案


在3.0.5.RELEASE中,这些注释在 HandlerMethodInvoker.resolveHandlerArguments ,如果没有提供任何值,Spring 将使用 RequestParam.value()。这可以返回空字符串。

再往下,Spring 使用 HandlerMethodInvoker.resolveRequestParam,其中,如果参数名称为空,则会调用 HandlerMethodINvoker.getRequiredParameterNameMethodParameter methodParam 作为参数:

718     private String getRequiredParameterName(MethodParameter methodParam) {
719     String name = methodParam.getParameterName();
720     if (name == null) {
721         throw new IllegalStateException(
722                 "No parameter name specified for argument of type [" + methodParam.getParameterType().getName() +
723                         "], and no parameter name information found in class file either.");
724     }
725     return name;
726 }

请注意,这里它尝试从 methodParam 中提取信息,如果我们备份树,我们会看到 resolveHandlerArguments 实际上创建了一个新的 MethodParameter 对于它处理的每个参数。在 MethodParameter 中,我们可以看一下 getParameterName()

276 public String getParameterName() {
277     if (this.parameterNameDiscoverer != null) {
278         String[] parameterNames = (this.method != null ?
279                 this.parameterNameDiscoverer.getParameterNames(this.method) :
280                 this.parameterNameDiscoverer.getParameterNames(this.constructor));
281         if (parameterNames != null) {
282             this.parameterName = parameterNames[this.parameterIndex];
283         }
284         this.parameterNameDiscoverer = null;
285     }
286     return this.parameterName;
287 }

所以这使用了名为 ParameterNameDiscoverer,但这是一个接口,我的跟踪没有显示它是哪个实现使用,有很少。查看 LocalVariableTableParameterNameDiscoverer.getParameterNames 我们最终调用了 LocalVariableTableParameterNameDiscoverer.ParameterNameDiscoveringVisitor 作为 org.objectweb.asm.ClassReader,据我所知,它尝试从字节码中读取参数名称。

The short version of this is that apparently the parameter names are being compiled in, if they weren't, you'd get an exception indicating that Spring MVC couldn't deduce the parameter name. That is, parameter names aren't always stored in the bytecode, but it seems like if they are, Spring will find them, if not, you need to specify them when you add the @RequestParam annotation.

Other details are available on this similar question and it's answers.


In 3.0.5.RELEASE, these annotations are processed in HandlerMethodInvoker.resolveHandlerArguments and it appears that if no value is supplied, Spring uses RequestParam.value(). This can return the empty string.

Further down, Spring uses HandlerMethodInvoker.resolveRequestParam, and inside there, if the parameter name is empty, it invokes HandlerMethodINvoker.getRequiredParameterName with MethodParameter methodParam as an argument:

718     private String getRequiredParameterName(MethodParameter methodParam) {
719     String name = methodParam.getParameterName();
720     if (name == null) {
721         throw new IllegalStateException(
722                 "No parameter name specified for argument of type [" + methodParam.getParameterType().getName() +
723                         "], and no parameter name information found in class file either.");
724     }
725     return name;
726 }

Note that here it tries to pull the information from methodParam, which, if we back up the tree, we see that resolveHandlerArguments actually creates a new MethodParameter for each argument that it processes. Inside MethodParameter, we can take a look at getParameterName():

276 public String getParameterName() {
277     if (this.parameterNameDiscoverer != null) {
278         String[] parameterNames = (this.method != null ?
279                 this.parameterNameDiscoverer.getParameterNames(this.method) :
280                 this.parameterNameDiscoverer.getParameterNames(this.constructor));
281         if (parameterNames != null) {
282             this.parameterName = parameterNames[this.parameterIndex];
283         }
284         this.parameterNameDiscoverer = null;
285     }
286     return this.parameterName;
287 }

So this uses something called a ParameterNameDiscoverer, but this is an interface and my trace isn't showing which implementation it's using, there are a few. Looking at LocalVariableTableParameterNameDiscoverer.getParameterNames we end up calling a LocalVariableTableParameterNameDiscoverer.ParameterNameDiscoveringVisitor as part of an org.objectweb.asm.ClassReader, which as far as I can tell tries to read the parameter name out of the bytecode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文