Spring MVC - Interceptor never called

后端 未结 6 1976
我寻月下人不归
我寻月下人不归 2021-02-15 15:06

I am trying to configure an interceptor in my application and I am not being able to make it work.

In my application configuration class, I have configured in the follow

6条回答
  •  孤城傲影
    2021-02-15 15:49

    If it´s possible. Use this approach:

    public class Application extends WebMvcConfigurerAdapter{
    ...
    @Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }
    
    public @Override void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor());
    }
    }
    

    instead of:

    @Bean
    public MappedInterceptor myInterceptor()
    {
        return new MappedInterceptor(null, new MyInterceptor());
    }
    

    because with the first you can use injection features (like @Autowired, etc...)

提交回复
热议问题