Interlated
Interlated

Reputation: 5926

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);
  }
}

Upvotes: 8

Views: 7476

Answers (1)

Alex Ciocan
Alex Ciocan

Reputation: 2322

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.

Upvotes: 7

Related Questions