Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With multiple Spring WebMvcConfigurerAdapter how can I control the order of the Configuration classes?

With 2 configurations, in different jar files I would like to control the order of interceptor registration. One interceptor potentially relies on data set by the other.

I have tried @Order on the addInterceptors method.

@Configuration
public class PipelineConfig extends WebMvcConfigurerAdapter {
  @Autowired
  @Qualifier("Audit")
  HandlerInterceptor auditInterceptor;

  public PipelineConfig() {
  }

  @Order(2)
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(this.auditInterceptor);
  }
}

And

@Configuration
public class ExecutionPipelineConfig extends WebMvcConfigurerAdapter {
  @Autowired
  @Qualifier("ExecutionContext")
  HandlerInterceptor executionContextInterceptor;

  public ExecutionPipelineConfig() {
  }

  @Order(1)
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(this.executionContextInterceptor);
  }
}
like image 388
Interlated Avatar asked Dec 07 '16 18:12

Interlated


People also ask

How do you order interceptors in spring boot?

React Full Stack Web Development With Spring Boot To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller.

Can I have multiple WebMvcConfigurer?

Also, you can have as many implementations of WebMvcConfigurer as you want in a Spring project. So, if you need to define some common interceptors in the Common module, you can extend the AbstractWebMvcConfigurerImpl in the common module also.

Which is the front controller class of Spring MVC?

DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.


1 Answers

The spring framework documentation [ http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ ] specifies that @Order is used for :

  • ordering instances in a collection
  • ordering executionListeners
  • @Configuration elements ( spring framework 4.2+)

The @Order annotation can be applied in your case at class level for your configurations if your spring version >4.2.

For example:

@Configuration
@Order(2)
public class PipelineConfig extends WebMvcConfigurerAdapter {

Also this coudld be a usecase for the @Import annotation for aggregating @Configuration files(http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html)

If, on the other hand, one of your interceptors potentially rely on data/beans you can use the @DependsOn("beanName") annotation.

like image 134
Alex Ciocan Avatar answered Oct 25 '22 06:10

Alex Ciocan