• Spring错误排查-No ServletContext set


    Bug 信息

    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

    java.lang.IllegalStateException: Failed to load ApplicationContext

    Caused by: java.lang.IllegalStateException: No ServletContext set

    相关文件

    SpringConfig.java

    @Configuration
    @ComponentScan(basePackages = "com.xxx", excludeFilters = @ComponentScan.Filter(
            type= FilterType.ANNOTATION, classes= Controller.class))
    @PropertySource({"classpath:jdbc.properties", "classpath:log4j.properties"})
    @Import({JdbcConfig.class, MybatisConfig.class})
    @EnableWebMvc
    public class SpringConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    SpringMvcConfig.java

    @Configuration
    @ComponentScan(basePackages = {"com.xxx.controller"})
    @Import(SpringMvcSupport.class)
    public class SpringMvcConfig {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试类

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfig.class)
    public class StudentServiceTest {
        @Autowired
        private StudentService studentService;
        @Test
        public void testGetById(){
            Student student = studentService.findStudentById(2);
            System.out.println(student);
            // 规范的做法是进行 assert 判断
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    情况说明

    事实上,在非测试环境,上述代码运行正常,但是进入测试阶段就出问题了。根据 Bug 信息搜索相关资料发现,问题集中在两个方面:

    1. @EnableWebMvc 注解使用。
    2. 容器的加载问题上。

    第一个问题,@EnableWebMvc 注解和 SpringMvc 相关,应该将其写在 SpringMvcConfig.java 文件上。
    第二个问题,在 SpringConfig.java 里,扫描了 com.xxx.config 包,也许产生了容器加载上的问题。因此在 ComponentScan 的写法上,可以采用常规写法,具体到每一个需要扫描的包。
    明确了这两个问题,可以将代码调整为:
    SpringConfig.java

    @Configuration
    @ComponentScan(basePackages = {"com.sugarian.service", "com.sugarian.dao"})
    @PropertySource({"classpath:jdbc.properties", "classpath:log4j.properties"})
    @Import({JdbcConfig.class, MybatisConfig.class})
    public class SpringConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    SpringMvcConfig.java

    @Configuration
    @ComponentScan(basePackages = {"com.xxx.controller"})
    @Import(SpringMvcSupport.class)
    @EnableWebMvc
    public class SpringMvcConfig {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    参考文章

    [1] Spring错误排查-No ServletContext set
    [2] stackoverflow

  • 相关阅读:
    智能时代的“发动机升级”:数据中心十年之变
    飞网,虚拟组网的神器(一):飞网的基本使用
    什么企业可以申报高新技术企业
    oracle重新安装集群软件后挂盘启动数据库
    移动Web:媒体查询及手机端PC端识别
    阿里二面:RocketMQ 消息积压了,增 加消费者有用吗?
    是否需要提高代码阅读能力?这有技巧
    java springboot儿童医药评价系统网站python
    stm32之30.DMA
    低代码平台上的出入库管理
  • 原文地址:https://blog.csdn.net/fatfish_/article/details/127983733