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

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

 

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

 

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


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

3.检查文件路径

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

正确的文件结构如下:

 

posted @   BerserkD  阅读(542)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示