Looking at the docs and several examples of @InitBinder online, I never see these methods marked as static. My IDE is claiming they can be marked as static, I don't see any errors while doing so, and conceptually I would think you can mark it this way.
Are there any downsides to doing so? I understand that the method must not return a value.
The code inside Spring Web that handles
@InitBinderannotation does not make any assumptions or requirements whether it is static or not.The difference between static and non-static
@InitBindermethod boils down to standard Java semantics of static and non-static methods, meaning the access to non-static variables and an ability to override the method.If you do not need the instance variables or methods, you can safely make
@InitBindermethod static, and also private.Regardless of whether the
@InitBindermethod static or not, it can be called multiple times per request, like at least once for each@PathVariable,@RequestParam,@RequestHeaderor@CookieValueparameter of the request processing method. You can check the target bound to theWebDataBinderobject usingWebDataBinder#getObjectName()and customize the binder configuration based on that information.