[英]Spring @initBinder method to be called only once inside a controller
I have a class that annotated with @RestController
and @ControllerAdvice
that has my requests mapped methods ( @RequestMapping
). 我有一个用@RestController
和@ControllerAdvice
注释的类, @RestController
具有我的请求映射的方法( @RequestMapping
)。 Also in this class I added a method public void initBinder(WebDataBinder dataBinder)
that is annotated with @InitBinder
and responsible to register some custom editor. 同样在此类中,我添加了一个方法public void initBinder(WebDataBinder dataBinder)
,该方法使用@InitBinder
注释,并负责注册一些自定义编辑器。 Specifically, it is a propertyEditor
that convert String to Enum. 具体来说,是将String转换为Enum的propertyEditor
。
I noticed that on every call to @RequestMapping
method in my controller, my initBinder method is being called. 我注意到在控制器中每次对@RequestMapping
方法的调用时,都会调用我的initBinder方法。 Since in my opinion this editor registration should happen only once (initialization of controller), I want this to be set (called) only once. 由于我认为该编辑器注册仅应发生一次(控制器初始化),因此我希望仅将其设置(调用一次)。
Is there a way to do so? 有办法吗?
Alternatively, you can use implementation of BindingInitializer for registering your custom Editor. 另外,您可以使用BindingInitializer的实现来注册自定义编辑器。 You have to define a bean of class "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" in your app context file and set its "webBindingInitializer" property with your implementation of BindingInitializer. 您必须在应用上下文文件中定义一个类为“ org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”的bean,并通过BindingInitializer的实现来设置其“ webBindingInitializer”属性。
In any case, scope of WebDataBinder is for a request. 无论如何,WebDataBinder的范围都是针对请求的。 Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.