Improve documentation on applicability of mapped interceptors with the Spring MVC config #31185
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: documentation
A documentation task
Milestone
Affects: 5.3.29 (most likely current as well)
In the documentation for Interceptors in Spring MVC the methods for adding them via
WebMvcConfigurer.addInterceptors(InterceptorRegistry)
and<mvc:interceptors/>
(in XML) are presented as equivalent, which is not strictly true.Interceptors registered via
<mvc:interceptors/>
are added asMappedInterceptor
s to the ApplicationContext to be later picked up byAbstractHandlerMapping.detectMappedInterceptors(List<HandlerInterceptor>)
(viainitApplicationContext()
) and added to the list of Interceptors.Interceptors configured via
WebMvcConfigurer.addInterceptors()
(orWebMvcConfigurationSupport
) will be directly set as Interceptors via a call toAbstractHandlerMapping.setInterceptors()
(inrequestMappingHandlerMapping()
).As a result,
FlowHandlerMapping
(and possibly others) will be aware of Interceptors if these are configured via XML, but not via recommended Java Config.As far as I understand it, there is one workaround and the possibility to make the configuration explicit.
Workaround
Instead of adding the Interceptor in
addInterceptors()
, a bean of typeMappedInterceptor
is defined in the configuration class:That way the Interceptor gets picked up the same way as being configured via
<mvc:interceptors/>
(while not being obvious).Explicit configuration
The Interceptor is configured as bean, added in
addInterceptors()
and available for usage in other handlers (in this example Spring Web FlowsFlowHandlerMapping
).Proposed Solution
To document this difference in behavior, I would propose another "Note" in the documentation. Something like:
Wishlist
I am aware of the falling out of favor of the "Open Session in View" pattern and prevalent usage of other web technologies not depending on server rendering and therefore Spring Web Flow. At the same time, I think there are many applications out there using these technologies and pattern. Therefore I would appreciate it, if you could use
OpenSessionInViewInterceptor
as example in your documentation (perhaps even mentioningFlowHandlerMapping
) to make it web searchable and obvious, that there is something to consider while switching from XML to Java ConfigThe text was updated successfully, but these errors were encountered: