spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Add support for detecting only local beans for DispatcherServlet

Open filiphr opened this issue 1 month ago • 5 comments

The goal of this PR is to be able to configure the DispatcherServlet to only look for beans in the local application context, i.e. do not look in all ancestors.

The PR is currently only doing this for the HandlerMapping(s). However, if this is something that is acceptable for the Spring Team then I can adjust the rest of the beans in the dispatcher servlet:

  • handlerAdapters
  • handlerExceptionResolvers
  • viewResolvers

The reason for this is that we are using different DispatcherServlet(s) to expose different REST APIs and with Spring Boot Actuator the actuator endpoints were being exposed in our servlets as well. We have remedied that using Spring Security and the AntPathMatcher to block access to **/actuator/*. However, the AntPathMatcher will no longer be there in Spring 7 and I wanted to find a better solution.

I did manage to find WebMvcEndpointChildContextConfiguration from Spring Boot, that is achieving exactly what we are looking for. However, I wanted to see if we can avoid the need of all those CompositeHandlerMapping, CompositeHandlerAdapter, etc. and let the DispatcherServlet itself handle this.

If this gets accepted then the different composites in Spring Boot can be removed, and the Actuator management DispatcherServlet would only need to change

dispatcherServlet.setDetectAllHandlerAdapters(false);
dispatcherServlet.setDetectAllHandlerExceptionResolvers(false);
dispatcherServlet.setDetectAllHandlerMappings(false);
dispatcherServlet.setDetectAllViewResolvers(false);

to something like

dispatcherServlet.detectLocalHandlerMappingsOnly()
dispatcherServlet.detectLocalHandlerExceptionResolversOnly();
dispatcherServlet.detectLocalHandlerMappingsOnly();
dispatcherServlet.detectLocalViewResolversOnly();

I'm also open to adjusting the naming of the methods as well. It does not have to be exactly like I've done in this PR.

Thanks for taking the time and considering this PR

filiphr avatar Nov 01 '25 00:11 filiphr