【发布时间】:2019-01-05 05:30:06
【问题描述】:
在调试 Spring Boot 应用程序时,我注意到每个传入请求都会调用带有 @InitBinder 注释的方法。
@InitBinder("categories")
public void bindFields(WebDataBinder binder) {
binder.registerCustomEditor(Set.class, new CustomPropertyEditor());
}
在@InitBinder 方法中,我们将PropertyEditor 设置为活页夹。我不明白为什么要一次又一次地调用这些方法并设置相同的东西?
Spring 是否为每个请求创建一个新的 WebDataBinder 对象?
【问题讨论】:
-
实际上在spring 5中,一个GET请求(一个POST请求相同)和带有@initBinder注解的方法被调用了3次!这是怎么回事???
-
啊,#3 这里其实是我的问题的答案forum.spring.io/forum/spring-projects/web/…
-
太棒了...论坛已经死了。
-
这是该论坛帖子的存档链接:web.archive.org/web/20181223143621if_/http://forum.spring.io/…。看起来这就是答案:“WebDataBinder 实例特定于模型属性。您可以像这样验证为数据绑定器创建的目标模型属性......鉴于您拥有的请求参数和模型属性的数量,什么您最可能想要做的是更具体地了解您的 InitBinder 方法适用于哪些对象。”
标签: java spring spring-boot data-binding spring-mvc-initbinders