When using Spring Web, in this case for rest endpoints & using Spring Boot 2, I can configure interceptors for my app using by implementing the WebMvcConfigurer interface:
@Configuration
public class SpringWebConfig implements WebMvcConfigurer
{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor).addPathPatterns("/api/endpoint/**");
}
}
I added this interceptor to most of my apps in an automatic fashion by doing the following:
- Create a "common-jar", and put the above interface under package
com.company.api. - In every app, add the package
com.company.apito the api scan.
This common package also contains the Interceptor and utility classes to make this interceptor work, so in effect, adding this common-jar would automatically add he interceptor to all operations in the app, which is a similar concept as to what Spring itself does: adding dependencies changes the default configuration of Spring.
The problem I'm facing now is this approach cannot be extended to a second interceptor in a second jar, because I already used the WebMvcConfigurer implementation. and I cannot have two.
I was thinking about maybe using some kind of composite-configurer pattern where we loop over every configurer, collect all interceptors, and then add them once, but unfortunately Spring doesn't allow this. What are my options?
Currently, the approach I took is duplicating the WebMvcConfigurer interface in every app that requires it. I feel sad when something changes, and I have to change the same snippet of code in every single app.
If I understand your question correctly , basically you want to define some common
Interceptorsin multiple JARs such that an application can activate theseInterceptorsby simply including these JARs into their app ?Actually , Spring has already implement this feature. When there are multiple
WebMvcConfigurerbeans , Spring simply loop them one by one and calls their configuration methods. So the end-result is thatInterceptorRegistrywill contain all interceptors.If the client application need to activate certain
WebMvcConfigureronly, it can simply exclude those JARs containing theWebMvcConfigurerthat they don't want.To take this idea further which allow the application to control which
Interceptorsto activate down to interceptor level , you could even do the following in each common JAR :In the client application , use
includeFilters/excludeFiltersin@ComponentScanto customise which to interceptors to include. For example, to disable certainInterceptors, you could do :