由于你使用的是Spring Boot,因此我假设你希望在可能的情况下依靠Spring的自动配置。要添加其他自定义配置(例如你的拦截器),只需提供一个配置或
WebMvcConfigurerAdapter。
这是一个配置类的例子:
@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter { @Autowired HandlerInterceptor yourInjectedInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(...) ... registry.addInterceptor(getYourInterceptor()); registry.addInterceptor(yourInjectedInterceptor); // next two should be avoid -- tightly coupled and not very testable registry.addInterceptor(new YourInterceptor()); registry.addInterceptor(new HandlerInterceptor() { ... }); }}


