如何获取默认的WebApplicationContext?

Wal*_*ker 8 java spring spring-mvc

我需要上下文ApplicationContext.xml,我提供的web.xml

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
 </listener> 
Run Code Online (Sandbox Code Playgroud)

但是我需要在Controller课堂上控制它.

我尝试了很多东西,包括

WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
Run Code Online (Sandbox Code Playgroud)

但它没有帮助.

ska*_*man 23

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

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

另见getRequiredWebApplicationContext().

  • @Walker:一个upvote和一个答案接受值得25谢谢你... (5认同)