如何获取默认的WebApplicationContext?
问题内容:
我需要的背景下ApplicationContext.xml
,这是我所提供web.xml
的
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
但是我需要在Controller
课堂上对此进行控制。
我尝试了很多事情,包括
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
但这没有帮助。
问题答案:
最简单的解决方案是使用getWebApplicationContext()
的WebApplicationContextUtils
:
查找此Web应用程序的根WebApplicationContext,该WebApplicationContext通常是通过ContextLoaderListener加载的。
另请参阅getRequiredWebApplicationContext()
。