I'm struggling to find a way to add request interceptors from within one or more add-on modules (modules being Maven modules in this case).
In the main module, there is a Web MVC configuration class that looks like this:
@Configuration
public class WebMvcConfig extends DelegatingWebMvcConfiguration {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// main module interceptors are registered here via
// registry.addInterceptor(interceptor);
}
}
Now, in add-on module 1 I have MyFirstCustomInterceptor and in add-on module 2 MySecondCustomInterceptor which I would like to add to the same interceptor registry. I feel like this should be easy but I couldn't find an obvious way to do it when reading through the official Spring MVC documentation.
One approach, that is mentioned in the documentation and that sounded promising, was to use the RequestMappingHandlerMapping bean and it's setInterceptors(Object[] interceptors) method.
I tried that by injecting the bean into an application-started event listener class and adding the custom interceptors through requestMappingHandlerMapping.setInterceptors(myCustomerInterceptorArray). Unfortunately, that didn't quite work. It seems the interceptor is being added but Spring uses another interceptor list - adaptedInterceptors - for the execution chain. Unfortunately, there don't seem to be any public methods available to add an interceptor to the adaptedInterceptors list.
I'm thinking that maybe the RequestMappingHandlerMapping.setInteceptors() method needs to be called earlier, or that there must be a way to extend the WebMvcConfig to the add-on module. But I'm unsure how that would be done.
Edit:
Another idea I just had, is based on injecting a list of all HandlerInterceptor beans. For example:
@Configuration
public class WebMvcConfig extends DelegatingWebMvcConfiguration {
@Inject private List<HandlerInterceptor> handlerInterceptors;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// Note: the order of the interceptors would likely be an issue here
for (HandlerInterceptor interceptor : handlerInterceptors) {
registry.addInterceptor(interceptor);
}
}
}
The only problem with this approach would be that there isn't a really good way to order the interceptors. That could be solved with a custom solution, like adding an order annotation on each interceptor class and taking that into account when adding them to the registry. But it still doesn't feel 100% clean. So I'm still hoping there is a better way.
DelegatingWebMvcConfigurationor extendingWebMvcConfigurerAdapterand add it. Just add this class to your module. Don't extendDelegatingWebMvcConfigurationin you root application just add a@Configurationclass that is annotated with@EnableWebMvcto customize your configuration you can use theWebMvcConfigurer. All the beans implementingWebMvcConfigurerare considered when the mvc stuff is configured.DelegatingWebMvcConfigurationinstead of using theEnableWebMvcannotation because I'm doing some advanced config in there. It was my understanding that having a@Configurationclass that extendsDelegatingWebMvcConfigurationis the equivalent of using@EnableWebMvc. I tried adding both a@Configurationclass that implementsWebMvcConfigurerand a bean that implements the same. But for neither one I see theaddInterceptorsmethod being called.DelegatingWebMvcConfigurationand override some of the method in there the delegating isn't happening anymore. Hence it isn't working. What kind of configuration are you doing? Generally you should not have to extend theDelegatingWebMvcConfigurationand you might want to split things up in adding interceptors and your advanced configuration (but maybe you can add the full configuration class).super.addInterceptors(registry)in the override method it looks like it's working as expected. I had to extendDelegatingWebMvcConfigurationa while back because of a limitation on how the message converters could be configured throughWebMvcConfigurerAdapterif I remember correctly. I will review and see if that is now unnecessary with the newest Spring version. Thanks again!