如何获取默认的WebApplicationContext?

我需要ApplicationContext.xml的上下文,我在web.xml中提供了它

   org.springframework.web.context.ContextLoaderListener   

但是我需要在Controller类中Controller

我尝试了很多东西,包括

 WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext(); 

但它没有帮助。

最简单的解决方案是使用WebApplicationContextUtils getWebApplicationContext()

查找此Web应用程序的根WebApplicationContext,通常通过ContextLoaderListener加载。

另请参见getRequiredWebApplicationContext()

RequestContextUtils将是最佳选择,即使您有多个WebAppContexts,它也将获得当前活动的。