詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
問題描述
今天在給SpringBoot項(xiàng)目配置攔截器的時候發(fā)現(xiàn)怎么都進(jìn)不到攔截器的方法里面,在搜索引擎上看了無數(shù)篇關(guān)于配置攔截器的文章都沒有找到解決方案。
就在我準(zhǔn)備放棄的時候,在 CSDN 上發(fā)現(xiàn)了一篇文章,說的是SpringBoot 用了@ImportResource 配置的攔截器就不起作用了。于是我就趕緊到Application
啟動類看了一眼,果然項(xiàng)目中使用了@ImportResource
注解用于配置系統(tǒng)的參數(shù)。
代碼如下:
啟動類配置
package com.xx.xxx; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @EnableDiscoveryClient @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, ThymeleafAutoConfiguration.class}) @SpringBootApplication // 注意這里 ?。。?! @ImportResource(locations={"classpath:config/application-*.xml"}) @EnableHystrix public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
攔截器配置
package com.xx.xxx.config; import com.example.springbootdemo.Interceptor.LoginInterceptor; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { /** * 攔截器(用戶登錄驗(yàn)證) * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns 用于添加攔截規(guī)則 // excludePathPatterns 用戶排除攔截 registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user","/login"); super.addInterceptors(registry); } }
攔截器實(shí)現(xiàn)
package com.xx.xxx.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor implements HandlerInterceptor { private final static Logger LOGGER = LoggerFactory.getLogger(LoginInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { LOGGER.info("******進(jìn)來了******"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
具體為什么使用@ImportResource注解會影響攔截器的配置,如果有機(jī)會研究一下源碼或許能夠找到答案。
PS : SpringBoot 版本 1.5.2
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單談?wù)刯ava的異常處理(Try Catch Finally)
在程序設(shè)計(jì)中,進(jìn)行異常處理是非常關(guān)鍵和重要的一部分。一個程序的異常處理框架的好壞直接影響到整個項(xiàng)目的代碼質(zhì)量以及后期維護(hù)成本和難度。2016-03-03java圖形化界面實(shí)現(xiàn)簡單混合運(yùn)算計(jì)算器的示例代碼
這篇文章主要介紹了java圖形化界面實(shí)現(xiàn)簡單混合運(yùn)算計(jì)算器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11SpringBoot + openFeign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用的過程
現(xiàn)在的微服務(wù)項(xiàng)目不少都使用的是springboot+spring cloud構(gòu)建的項(xiàng)目,微服務(wù)之間的調(diào)用都離不開feign來進(jìn)行遠(yuǎn)程調(diào)用,這篇文章主要介紹了SpringBoot + openFeign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用,需要的朋友可以參考下2022-11-11java虛擬機(jī)深入學(xué)習(xí)之內(nèi)存管理機(jī)制
java虛擬機(jī)在程序運(yùn)行時將內(nèi)存劃分為多個區(qū)域,每個區(qū)域作用,生命周期各不相同,下面這篇文章主要給大家介紹了關(guān)于java虛擬機(jī)深入學(xué)習(xí)之內(nèi)存管理機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-11-11