【spring】使用WebApplicationContextUtils.getWebApplicationContext获取上下文对象时抛出空指针异常


1.检查异常点,打印传入的参数servletContext,正常。

ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);

2.检查上下文监听器,监听器填写无误。



contextConfigLocation
classpath:applicationContext.xml



org.springframework.web.context.ContextLoaderListener

3.检查文件路径

发现webapp没有被识别为web项目,仔细查看后其实不是没有被识别为web项目,而是在main文件夹外面还有一个webapp,把真正可用的webapp放回main文件夹里,恢复正常。

正确的文件结构如下: