详解SpringBoot开发使用@ImportResource注解影响拦截器

2025-05-29 0 72

问题描述

今天在给springboot项目配置拦截器的时候发现怎么都进不到拦截器的方法里面,在搜索引擎上看了无数篇关于配置拦截器的文章都没有找到解决方案。

就在我准备放弃的时候,在 csdn 上发现了一篇文章,说的是springboot 用了@importresource 配置的拦截器就不起作用了。于是我就赶紧到application启动类看了一眼,果然项目中使用了@importresource 注解用于配置系统的参数。

代码如下:

启动类配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
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);

}

}

拦截器配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
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 用于添加拦截规则

// excludepathpatterns 用户排除拦截

registry.addinterceptor(new logininterceptor()).addpathpatterns("/**").excludepathpatterns("/user","/login");

super.addinterceptors(registry);

}

}

拦截器实现

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27
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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/yuansc/p/9077509.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解SpringBoot开发使用@ImportResource注解影响拦截器 https://www.kuaiidc.com/110755.html

相关文章

发表评论
暂无评论