请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

springBoot拦截器失效

更具老师的代码在自己写练手项目的时候发现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 00:46:45

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

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

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_水無月流歌_0 #1
    粗略的搜看了一下,感觉2.0的配置和1.0的配置差别还是挺大的!坑也是挺多的!老师现在企业中用1.x的居多还是大家在慢慢的转换为2.x呢?
    回复 有任何疑惑可以回复我~ 2019-04-06 15:06:35
  • 翔仔 回复 提问者 qq_水無月流歌_0 #2
    企业里面我们目前还在用1.5x的,一般企业喜欢用已知稳定的框架,新上的可能坑比较多不稳定。配置上差别其实还好,咱们先保证功能搞定。建议后续搞定后再调研下2.0如何添加上去,同学也可以总结点变成2.0的心得分享出来:)
    回复 有任何疑惑可以回复我~ 2019-04-07 00:55:07
  • 提问者 qq_水無月流歌_0 回复 翔仔 #3
    开了一个帖记录一下!嘿嘿
    https://www.imooc.com/article/284656
    回复 有任何疑惑可以回复我~ 2019-04-08 16:09:04
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信