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

With two configurations in different jar files, I would like to control the interceptor registration order. One interceptor is potentially dependent on data set by another.

I tried @Order using 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);
  }
}
+4
source share
1 answer

The spring framework documentation [ http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ ] indicates what is @Orderused for:

  • order of instances in the collection
  • streamlining execution
  • @ Configuration items (spring framework 4.2+)

@Order , spring > 4.2.

:

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

, coudld usecase @Import @Configuration (http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html)

, , / beans, @DependsOn("beanName").

+4

Source: https://habr.com/ru/post/1663155/


All Articles