Spring WebApplicationContextIls.getWebApplicationContext(servletContext)返回null
我已经建立了一个SpringDispatcherServlet但是当我打电话时Spring WebApplicationContextIls.getWebApplicationContext(servletContext)返回null,spring,spring-mvc,jakarta-ee,Spring,Spring Mvc,Jakarta Ee,我已经建立了一个SpringDispatcherServlet但是当我打电话时 WebApplicationContextUtils.getWebApplicationContext(MoJV.servletContext) 它返回空值 我错过了什么?如果你说你在什么时候打电话,如果应用程序正常运行,也许有人会有主意…@SergeBallesta我是从控制器上打电话的,所以我相信那时一切都应该准备好了..什么是MoJV.servletContext?它是否返回ServletContext?
WebApplicationContextUtils.getWebApplicationContext(MoJV.servletContext)
它返回空值
我错过了什么?如果你说你在什么时候打电话,如果应用程序正常运行,也许有人会有主意…@SergeBallesta我是从控制器上打电话的,所以我相信那时一切都应该准备好了..什么是MoJV.servletContext?它是否返回ServletContext?如果需要应用程序上下文,只需@autowire it:
@Autowired private ApplicationContext appContext代码>是,自动接线工作。应该删除该方法调用!刚刚花了2个小时:(webapplicationcontextitils
返回根应用程序上下文,ContextLoaderListener
加载的根应用程序上下文,如果您没有,它将返回null
。Intead使用requestContextitils
获取当前DispatcherServlet
的应用程序上下文。这也在javadoc。这里有一点解释就好了。尽量避免只发布代码。谢谢!不是真的。有些人可能不熟悉@Autowired
。这不是一个非常描述性的问题答案,特别是因为它不能解决getWebApplicationContext返回null的问题,但是尝试通过一种不同的机制,这种机制在所有情况下都不起作用。
@Autowired
private ApplicationContext applicationContext;