最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器

 更新時間:2018年11月26日 14:28:26   作者:袁老板  
這篇文章主要介紹了詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

問題描述

今天在給SpringBoot項目配置攔截器的時候發(fā)現(xiàn)怎么都進不到攔截器的方法里面,在搜索引擎上看了無數(shù)篇關于配置攔截器的文章都沒有找到解決方案。

就在我準備放棄的時候,在 CSDN 上發(fā)現(xiàn)了一篇文章,說的是SpringBoot 用了@ImportResource 配置的攔截器就不起作用了。于是我就趕緊到Application啟動類看了一眼,果然項目中使用了@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 {

  /**
   * 攔截器(用戶登錄驗證)
   * @param registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    // addPathPatterns 用于添加攔截規(guī)則
    // excludePathPatterns 用戶排除攔截
    registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user","/login");
    super.addInterceptors(registry);
  }
}

攔截器實現(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("******進來了******");
    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注解會影響攔截器的配置,如果有機會研究一下源碼或許能夠找到答案。

PS : SpringBoot 版本 1.5.2

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • java8中的HashMap原理詳解

    java8中的HashMap原理詳解

    這篇文章主要介紹了java8中的HashMap原理詳解,HashMap是日常開發(fā)中非常常用的容器,HashMap實現(xiàn)了Map接口,底層的實現(xiàn)原理是哈希表,HashMap不是一個線程安全的容器,需要的朋友可以參考下
    2023-09-09
  • Java中的JPA實體關系:JPA一對一,一對多(多對一),多對多

    Java中的JPA實體關系:JPA一對一,一對多(多對一),多對多

    Java Persistence API(JPA)是Java平臺上的一個對象關系映射(ORM)規(guī)范,用于簡化數(shù)據(jù)庫操作,其中實體關系的映射是核心內(nèi)容之一,本文將深入淺出地探討JPA中的三種基本實體關系類型:一對一、一對多、多對多,揭示常見問題、易錯點及其避免策略,希望能幫助大家
    2024-06-06
  • IDEA中打jar包的2種方式(Maven打jar包)

    IDEA中打jar包的2種方式(Maven打jar包)

    這篇文章主要給大家介紹了關于IDEA中打jar包的2種方式,分別是不使用Maven直接打Jar包與使用Maven打jar包的兩種方法,需要的朋友可以參考下
    2021-05-05
  • Spring?Boot整合阿里開源中間件Canal實現(xiàn)數(shù)據(jù)增量同步

    Spring?Boot整合阿里開源中間件Canal實現(xiàn)數(shù)據(jù)增量同步

    這篇文章主要為大家介紹了Spring?Boot整合阿里開源中間件Canal實現(xiàn)數(shù)據(jù)增量同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • 關于Mybatis使用collection分頁問題

    關于Mybatis使用collection分頁問題

    項目中mybatis分頁的場景是非常高頻的,當使用ResultMap并配置collection做分頁的時候,我們可能會遇到獲取當前頁的數(shù)據(jù)少于每頁大小的數(shù)據(jù)問題。接下來通過本文給大家介紹Mybatis使用collection分頁問題,感興趣的朋友一起看看吧
    2021-11-11
  • java構造方法的作用總結

    java構造方法的作用總結

    在本篇文章里小編給大家整理了關于java構造方法的相關知識點以及實例代碼,有需要的朋友們可以學習下。
    2019-07-07
  • 參數(shù)校驗Spring的@Valid注解用法解析

    參數(shù)校驗Spring的@Valid注解用法解析

    這篇文章主要介紹了參數(shù)校驗Spring的@Valid注解用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java中的List接口實現(xiàn)類LinkList和ArrayList詳解

    Java中的List接口實現(xiàn)類LinkList和ArrayList詳解

    這篇文章主要介紹了Java中的List接口實現(xiàn)類LinkList和ArrayList詳解,List接口繼承自Collection接口,是單列集合的一個重要分支,實現(xiàn)了List接口的對象稱為List集合,在List集合中允許出現(xiàn)重復的元素,所有的元素是以一種線性方式進行存儲的,需要的朋友可以參考下
    2024-01-01
  • 在SpringBoot 中從application.yml中獲取自定義常量方式

    在SpringBoot 中從application.yml中獲取自定義常量方式

    這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • SpringBoot圖文并茂講解登錄攔截器

    SpringBoot圖文并茂講解登錄攔截器

    其實spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關于如何在Springboot實現(xiàn)登陸攔截器功能的相關資料,需要的朋友可以參考下
    2022-06-06

最新評論

嵊泗县| 吴江市| 岢岚县| 汽车| 石台县| 汽车| 丰宁| 洪湖市| 宿州市| 大兴区| 忻城县| 闽清县| 宾川县| 张家川| 巩义市| 蒙山县| 仁化县| 新晃| 沁源县| 孟津县| 栾城县| 静海县| 保亭| 新蔡县| 兴隆县| 乳山市| 霞浦县| 梁平县| 光泽县| 安陆市| 霍城县| 怀仁县| 贵南县| 马关县| 新泰市| 泸州市| 万盛区| 南昌县| 伊金霍洛旗| 徐水县| 浠水县|