Java 使用@AutoConfigureAfter(WebMvcAutoConfiguration.class)和注释驱动时无法调用侦听器
在spring boot应用程序中实现拦截器时,我遇到了一个奇怪的问题,如下所示: 我的应用程序类别代码:Java 使用@AutoConfigureAfter(WebMvcAutoConfiguration.class)和注释驱动时无法调用侦听器,java,spring,spring-boot,interceptor,spring-annotations,Java,Spring,Spring Boot,Interceptor,Spring Annotations,在spring boot应用程序中实现拦截器时,我遇到了一个奇怪的问题,如下所示: 我的应用程序类别代码: @Import(CommonConfig.class) @EnableAsync @SpringBootApplication @EnableSwagger2 @EnableScheduling @EnableSpringDataWebSupport @ImportResource({ "classpath:META-INF/applicationContext
@Import(CommonConfig.class)
@EnableAsync
@SpringBootApplication
@EnableSwagger2
@EnableScheduling
@EnableSpringDataWebSupport
@ImportResource({ "classpath:META-INF/applicationContext.xml" })
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class Application extends SpringBootServletInitializer {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
.......
................
}
@Bean
public TokenInterceptor intializeTokenInterceptor() {
System.out.println("Adding interceptors");
return new TokenInterceptor();
}
@Bean
public WebMvcConfigurerAdapter adapter() {
return new WebMvcConfigurerAdapter() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
System.out.println("Adding interceptors");
registry.addInterceptor(intializeTokenInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
};
}
}
在我的应用程序上下文中,我提到了标记(mvc:annotation-driven),因为我需要它来初始化bean类
现在,如果我通过删除注释驱动标记来测试我的应用程序,我的拦截器将被调用。但是,如果我保留这个标记,我的拦截器就不会被调用(我相信这会以某种方式覆盖WebMVCConfigureAdapterBean从WebMvcAutoConfiguration.class的实现) 删除标记并
@EnableWebMvc
您正在使用Spring Boot,但通过添加显式配置禁用所有自动配置功能。这同样适用于@AutoConfigureAfter
删除它,因为它不添加任何内容。最后,为什么还要加载xml文件?我有应用程序上下文,因为我有与安全相关的配置,还有MongoBean配置,为什么需要xml?Java也可以很好地实现这一点。除此之外,Spring Boot还可以为您自动配置mongo。尽管如此,您还是通过尝试自己配置来禁用自动web配置。删除那些(如我在初始评论中所述),只注册拦截器。