简体   繁体   English

为什么每次请求都会调用@InitBinder 方法?

[英]Why the @InitBinder method is called for every request?

While I was debugging my Spring Boot application I noticed that methods annotated with @InitBinder are invoked for every incoming request.在调试 Spring Boot 应用程序时,我注意到使用@InitBinder注释的方法会为每个传入请求调用。

@InitBinder("categories")
public void bindFields(WebDataBinder binder) {
    binder.registerCustomEditor(Set.class, new CustomPropertyEditor());
}

In @InitBinder methods we are setting a PropertyEditor to a binder.@InitBinder方法中,我们将PropertyEditor设置为绑定器。 I can't understand why should these methods be called again and again and set the same thing?我不明白为什么要一次又一次地调用这些方法并设置相同的东西?
Does Spring create a new WebDataBinder object for every single request? Spring 是否为每个请求创建一个新的WebDataBinder对象?

@InitBinder plays the role to identify the methods which used to initialize WebDataBinder . @InitBinder的作用是识别用于初始化WebDataBinder的方法。 Initbinder is usually used to bind requestParams to custom objects. Initbinder 通常用于将 requestParams 绑定到自定义对象。

Suppose your REST controller is annotated with @InitBinder , every request is handled within that controller will instantiate Initbinder and WebDatabinder will bind the request params to JavaBean objects.假设您的 REST 控制器使用@InitBinder注释,在该控制器内处理的每个请求都将实例化 Initbinder 并且WebDatabinder将请求参数绑定到 JavaBean 对象。

It provides methods to assign our validator classes.它提供了分配验证器类的方法。 Using addValidators() and setValidator() methods, we can assign our validators instances.使用addValidators()setValidator()方法,我们可以分配我们的验证器实例。

Use Case: Suppose Sun, Jan 20 is in the request param and you want to have a LocalDate Object parsed everytime from request parm.用例:假设 Sun, Jan 20 在请求参数中,并且您希望每次从请求参数中解析一个LocalDate对象。 You can add that parser logic within WebDatabinder and have that date validated/parsed everytime the request is made.您可以在WebDatabinder添加该解析器逻辑,并在每次发出请求时验证/解析该日期。

Reference: What is the purpose of init binder in spring MVC参考: spring MVC中init binder的作用是什么

It looks like this was answered in a post by Rossen Stoyanchev in the now defunct Spring Forums: https://web.archive.org/web/20181223143621if_/http://forum.spring.io/forum/spring-projects/web/55552-why-does-initbinder-method-get-called-multiple-times : Rossen Stoyanchev 在现已不存在的Spring 论坛中的帖子中似乎对此进行了回答: https ://web.archive.org/web/20181223143621if_/http://forum.spring.io/forum/spring-projects/web /55552-why-does-initbinder-method-get-Called-multiple-times

A WebDataBinder instance is specific to a model attribute. WebDataBinder 实例特定于模型属性。 You can verify the target model attribute a data binder is created for like this:您可以验证创建数据绑定器的目标模型属性,如下所示:

Code:代码:

 @InitBinder public void initBinder(WebDataBinder binder) { System.out.println("A binder for object: " + binder.getObjectName()); }

Data binders are also used for @RequestParam's and by default an init-binder method is used for for all model attribute and request parameters.数据绑定器也用于@RequestParam,默认情况下,init-binder 方法用于所有模型属性和请求参数。

Given the number of request parameters and model attributes you have, what you most likely want to do is be more specific about which objects your InitBinder method applies to.鉴于您拥有的请求参数和模型属性的数量,您最有可能想要做的是更具体地说明您的 InitBinder 方法适用于哪些对象。 For example:例如:

Code:代码:

 @InitBinder("tasks") public void initBinder(WebDataBinder binder) { System.out.println("A binder for object: " + binder.getObjectName()); }

There was also this follow-up question:还有这个后续问题:

Does it mean that we may specify for which command object the binder will be applied?这是否意味着我们可以指定绑定将应用于哪个命令对象? Let's say if we have multi-action controller which deals with 2 different domain object User which is represented by "user" and Report "report" then: @InitBinder("user") annotated method will be called only when binding the User object and the @InitBinder("report") only when binding Report?假设我们有处理由“user”和报告“report”表示的 2 个不同域对象 User 的多动作控制器,那么:@InitBinder("user") 注释方法将仅在绑定 User 对象和@InitBinder("report") 只在绑定 Report 时?

Yes, it means that's the model attribute or request parameter to which this specific data binding customization will apply.是的,这意味着该特定数据绑定自定义将应用到模型属性或请求参数。 You can also provide an array of names.您还可以提供名称数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM