SpringBoot自定义servlet容器配置后,启动报错以及不生效的问题
最近在学习springboot,看到定制servlet容器配置这里.当通过编码方式去设置server的属性时
1.
此种情况是继承了WebMvcConfigurationSupport,直接报错起不来,
2.以上为题一直没有解决,后来发现添加注解@EnableWebMVC全面接管后,错误解决,但是不知道是啥原理,而且修改的配置未生效.

3.网上搜索后https://blog.csdn.net/l1336037686/article/details/81047312,按照大佬的方法,去掉继承WebMvcConfigurationSupport后,启动不报错,但是修改的配置未生效.

以上有没有大佬知道是啥原因的?解答下,谢谢了.
下面是测试代码,springBoot版本2.2.6.RELEASE:
1.

此种情况是继承了WebMvcConfigurationSupport,直接报错起不来,
报错信息:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [com/practice/springbootweb/config/MyMvcConfig.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 ServletContext set
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at com.practice.springbootweb.SpringbootWebApplication.main(SpringbootWebApplication.java:10) [classes/:na]
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 ServletContext set
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
... 19 common frames omitted
Caused by: java.lang.IllegalStateException: No ServletContext set
at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:534) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at com.practice.springbootweb.config.MyMvcConfig$$EnhancerBySpringCGLIB$$dac75723.CGLIB$resourceHandlerMapping$9(<generated>) ~[classes/:na]
at com.practice.springbootweb.config.MyMvcConfig$$EnhancerBySpringCGLIB$$dac75723$$FastClassBySpringCGLIB$$ca627b0a.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at com.practice.springbootweb.config.MyMvcConfig$$EnhancerBySpringCGLIB$$dac75723.resourceHandlerMapping(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
... 20 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:50412', transport: 'socket'
Process finished with exit code 1
2.以上为题一直没有解决,后来发现添加注解@EnableWebMVC全面接管后,错误解决,但是不知道是啥原理,而且修改的配置未生效.

3.网上搜索后https://blog.csdn.net/l1336037686/article/details/81047312,按照大佬的方法,去掉继承WebMvcConfigurationSupport后,启动不报错,但是修改的配置未生效.

以上有没有大佬知道是啥原因的?解答下,谢谢了.
下面是测试代码,springBoot版本2.2.6.RELEASE:
/**
* MVC配置类
*/
@Configuration
//@EnableWebMvc
public class MyMvcConfig extends WebMvcConfigurationSupport {
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(8081);
return factory;
}
}
...全文
相关推荐
SpringBoot3 使用Druid报错,配置不生效,404报错
在SpringBoot3中使用Druid数据源,导致启动报错,小伙伴们因踩坑,不得不回退到SpringBoot2版本,好不容易解决了,结果配置又不生效。。。开开心心打开浏览器,输入druid地址,404报错。。。本文将解决小伙伴们的疑点,本文就是那一道光!
SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程图解
主要介绍了SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Tomcat启动springboot项目war包报错:启动子级时出错的问题
主要介绍了Tomcat启动springboot项目war包报错:启动子级时出错的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
解决SpringBoot启动过后不能访问jsp页面的问题(超详细)
主要介绍了解决SpringBoot启动过后不能访问jsp页面的问题,文中通过示例代码介绍的非常详细,有需要的朋友可以参考一下,希望对你有所帮助。
SpringBoot之配置嵌入式Servlet容器.pdf
SpringBoot配置嵌入式Servlet容器;注册Servlet三大组件;嵌入式Servlet容器自动配置原理以及启动原理


Java SE
