詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
問題描述
今天在給SpringBoot項(xiàng)目配置攔截器的時(shí)候發(fā)現(xiàn)怎么都進(jìn)不到攔截器的方法里面,在搜索引擎上看了無(wú)數(shù)篇關(guān)于配置攔截器的文章都沒有找到解決方案。
就在我準(zhǔn)備放棄的時(shí)候,在 CSDN 上發(fā)現(xiàn)了一篇文章,說的是SpringBoot 用了@ImportResource 配置的攔截器就不起作用了。于是我就趕緊到Application啟動(dòng)類看了一眼,果然項(xiàng)目中使用了@ImportResource 注解用于配置系統(tǒng)的參數(shù)。
代碼如下:
啟動(dòng)類配置
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注解會(huì)影響攔截器的配置,如果有機(jī)會(huì)研究一下源碼或許能夠找到答案。
PS : SpringBoot 版本 1.5.2
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的JPA實(shí)體關(guān)系:JPA一對(duì)一,一對(duì)多(多對(duì)一),多對(duì)多
Java Persistence API(JPA)是Java平臺(tái)上的一個(gè)對(duì)象關(guān)系映射(ORM)規(guī)范,用于簡(jiǎn)化數(shù)據(jù)庫(kù)操作,其中實(shí)體關(guān)系的映射是核心內(nèi)容之一,本文將深入淺出地探討JPA中的三種基本實(shí)體關(guān)系類型:一對(duì)一、一對(duì)多、多對(duì)多,揭示常見問題、易錯(cuò)點(diǎn)及其避免策略,希望能幫助大家2024-06-06
Spring?Boot整合阿里開源中間件Canal實(shí)現(xiàn)數(shù)據(jù)增量同步
這篇文章主要為大家介紹了Spring?Boot整合阿里開源中間件Canal實(shí)現(xiàn)數(shù)據(jù)增量同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
關(guān)于Mybatis使用collection分頁(yè)問題
項(xiàng)目中mybatis分頁(yè)的場(chǎng)景是非常高頻的,當(dāng)使用ResultMap并配置collection做分頁(yè)的時(shí)候,我們可能會(huì)遇到獲取當(dāng)前頁(yè)的數(shù)據(jù)少于每頁(yè)大小的數(shù)據(jù)問題。接下來通過本文給大家介紹Mybatis使用collection分頁(yè)問題,感興趣的朋友一起看看吧2021-11-11
參數(shù)校驗(yàn)Spring的@Valid注解用法解析
這篇文章主要介紹了參數(shù)校驗(yàn)Spring的@Valid注解用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java中的List接口實(shí)現(xiàn)類LinkList和ArrayList詳解
這篇文章主要介紹了Java中的List接口實(shí)現(xiàn)類LinkList和ArrayList詳解,List接口繼承自Collection接口,是單列集合的一個(gè)重要分支,實(shí)現(xiàn)了List接口的對(duì)象稱為L(zhǎng)ist集合,在List集合中允許出現(xiàn)重復(fù)的元素,所有的元素是以一種線性方式進(jìn)行存儲(chǔ)的,需要的朋友可以參考下2024-01-01
在SpringBoot 中從application.yml中獲取自定義常量方式
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04

