springBoot拦截器失效

来源:11-14 添加拦截器下

qq_水無月流歌_0

2019-04-04

更具老师的代码在自己写练手项目的时候发现MvcConfiguration的继承类的WebMvcConfigurerAdapter在springBoot2.1.3中已经被弃用代替的是WebMvcConfigurationSupport
WebMvcConfigurerAdapter被弃用

但是替换为WebMvcConfigurationSupport后发现拦截器失效!
通过百度发现原因

由于@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法,而这里上面恰恰使用了@EnableWebMvc,因此重写的方法都失效了

但是去掉EnableWebMvc注解后再次启动报错!错误如下

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-04 16:17:41.663 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [com/apa7/wzqd/config/web/MvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ApplicationContext set
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1305)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1144)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
	at com.apa7.wzqd.WzqdApplication.main(WzqdApplication.java:14)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ApplicationContext set
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
	... 19 common frames omitted
Caused by: java.lang.IllegalStateException: No ApplicationContext set
	at org.springframework.util.Assert.state(Assert.java:73)
	at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:485)
	at com.apa7.wzqd.config.web.MvcConfiguration$$EnhancerBySpringCGLIB$$48e15bee.CGLIB$resourceHandlerMapping$39(<generated>)
	at com.apa7.wzqd.config.web.MvcConfiguration$$EnhancerBySpringCGLIB$$48e15bee$$FastClassBySpringCGLIB$$1509930.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
	at com.apa7.wzqd.config.web.MvcConfiguration$$EnhancerBySpringCGLIB$$48e15bee.resourceHandlerMapping(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 20 common frames omitted

所以想请问一下老师如果继承WebMvcConfigurationSupport才能事程序正常运行?
或者说在WebMvcConfigurerAdapter已经被弃用后如何配置MvcConfiguration类?

写回答

1回答

翔仔

2019-04-05

同学好,2.0的迁移我还没有仔细研究,之前都是看了源码之后才搞的,最近都在加班也没太多时间去看,估计下半年左右会推出升级2.0的版本,因此建议同学如果还没有解决此类问题能力之前也可以继续沿用1.5的springboot。如果实在需要升级,建议了解完这几个类的关系后尝试

https://www.cnblogs.com/sufferingStriver/p/9026764.html

0
4
翔仔
回复
qq_水無月流歌_0
赞同学的总结能力:)
2019-04-08
共4条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程