Java Spring@initBinder方法在控制器内仅调用一次

Java Spring@initBinder方法在控制器内仅调用一次,java,spring,spring-mvc,propertyeditor,spring-mvc-initbinders,Java,Spring,Spring Mvc,Propertyeditor,Spring Mvc Initbinders,我有一个用@RestController和@ControllerAdvice注释的类,它有我的请求映射方法(@RequestMapping)。同样在这个类中,我添加了一个方法public void initBinder(WebDataBinder dataBinder),该方法用@initBinder注释,并负责注册一些自定义编辑器。 具体来说,它是一个将字符串转换为枚举的属性编辑器 我注意到在控制器中每次调用@RequestMapping方法时,都会调用initBinder方法。因为在我看来,

我有一个用
@RestController
@ControllerAdvice
注释的类,它有我的请求映射方法(
@RequestMapping
)。同样在这个类中,我添加了一个方法
public void initBinder(WebDataBinder dataBinder)
,该方法用
@initBinder
注释,并负责注册一些自定义编辑器。 具体来说,它是一个将字符串转换为枚举的
属性编辑器

我注意到在控制器中每次调用
@RequestMapping
方法时,都会调用initBinder方法。因为在我看来,编辑器注册应该只发生一次(控制器初始化),所以我希望只设置(调用)一次


有办法吗?

或者,您可以使用BindingInitializer的实现来注册自定义编辑器。您必须在应用程序上下文文件中定义一个类为“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”的bean,并在BindingInitializer实现中设置其“webBindingInitializer”属性


在任何情况下,WebDataBinder的作用域都是用于请求的。希望这能有所帮助。

AnnotationMethodHandlerAdapter已被弃用。从Spring 3.2开始,支持RequestMappingHandlerAdapter