详解SpringBoot开发使用@ImportResource注解影响拦截器
问题描述
今天在给SpringBoot项目配置拦截器的时候发现怎么都进不到拦截器的方法里面,在搜索引擎上看了无数篇关于配置拦截器的文章都没有找到解决方案。
就在我准备放弃的时候,在CSDN上发现了一篇文章,说的是SpringBoot用了@ImportResource配置的拦截器就不起作用了。于是我就赶紧到Application启动类看了一眼,果然项目中使用了@ImportResource注解用于配置系统的参数。
代码如下:
启动类配置
packagecom.xx.xxx; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.EnableAutoConfiguration; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.boot.builder.SpringApplicationBuilder; importorg.springframework.boot.web.servlet.ServletComponentScan; importorg.springframework.boot.web.support.SpringBootServletInitializer; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.ImportResource; @EnableDiscoveryClient @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, ThymeleafAutoConfiguration.class}) @SpringBootApplication //注意这里!!!! @ImportResource(locations={"classpath:config/application-*.xml"}) @EnableHystrix publicclassApplicationextendsSpringBootServletInitializer{ publicstaticvoidmain(String[]args){ SpringApplication.run(Application.class,args); } }
拦截器配置
packagecom.xx.xxx.config; importcom.example.springbootdemo.Interceptor.LoginInterceptor; importorg.springframework.boot.autoconfigure.EnableAutoConfiguration; importorg.springframework.cache.annotation.EnableCaching; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.ComponentScan; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.ImportResource; importorg.springframework.web.servlet.config.annotation.InterceptorRegistry; importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration publicclassWebMvcConfigextendsWebMvcConfigurerAdapter{ /** *拦截器(用户登录验证) *@paramregistry */ @Override publicvoidaddInterceptors(InterceptorRegistryregistry){ //addPathPatterns用于添加拦截规则 //excludePathPatterns用户排除拦截 registry.addInterceptor(newLoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user","/login"); super.addInterceptors(registry); } }
拦截器实现
packagecom.xx.xxx.interceptor; importorg.springframework.stereotype.Component; importorg.springframework.web.servlet.HandlerInterceptor; importorg.springframework.web.servlet.ModelAndView; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; publicclassLoginInterceptorimplementsHandlerInterceptor{ privatefinalstaticLoggerLOGGER=LoggerFactory.getLogger(LoginInterceptor.class); @Override publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{ LOGGER.info("******进来了******"); returntrue; } @Override publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{ } @Override publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{ } }
具体为什么使用@ImportResource注解会影响拦截器的配置,如果有机会研究一下源码或许能够找到答案。
PS:SpringBoot版本1.5.2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。