Alex
Alex

Reputation: 1817

addInterceptors is not called in spring boot 2.1.1 auto configuration

I am writing a spring-boot start which aims at auto configuring interceptors for handlers. The main class looks like the following:

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(MetricsProperties.class)
public class MetricsConfiguration implements WebMvcConfigurer {
    private static Logger logger = LoggerFactory.getLogger(MetricsConfiguration.class);

    private final MetricsProperties metricsProperties;

    public MetricsConfiguration(MetricsProperties properties) {
        this.metricsProperties = properties;
    }

    @Bean
    @Order(0)
    public MetricsCenter createMetricsCenter() {
        MetricsCenter metricsCenter = MetricsCenter.getInstance();
        metricsCenter.init(metricsProperties);
        return metricsCenter;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        logger.error("alex add");   // the log does not appear!!
        registry.addInterceptor(new MetricsInterceptor());
    }
}

according to the document

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

which says:

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

I did as what the document says, but the addInterceptors is not called and my interceptor is not equipped with spring mvc.

Can someone help me out of here, please?

thx.

Upvotes: 3

Views: 2159

Answers (1)

Asgar
Asgar

Reputation: 109

You may have a custom @Configuration class (e.g. in the consumer of your starter) extending WebMvcConfigurationSupport class in which case it takes the full control and your WebMvcConfigurers don't get called.

Upvotes: 9

Related Questions