java - 如何获取默认的 WebApplicationContext?

标签 java spring spring-mvc

我需要 ApplicationContext.xml 的上下文,我在 web.xml 中将其提供为

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

但是我需要在 Controller 类中对此进行控制。

我尝试了很多东西,包括

WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();

但这并没有帮助。

最佳答案

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

Find the root WebApplicationContext for this web application, which is typically loaded via ContextLoaderListener.

另见 getRequiredWebApplicationContext() .

关于java - 如何获取默认的 WebApplicationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495741/

相关文章:

spring - 使用 Spring Boot 和 MySQL 保存图像

java - Spring mvc从登录用户获取用户id

java - 实现java集合不丢失信息

Java 字符串不变性和使用相同的字符串值创建新字符串

spring - Grails-Spring Security- session 设置

spring - 通过curl路由上传到Amazon S3

java - 使用 Spring 自动配置自定义 @Qualifier

java - 如何在selenium webdriver的多选框中选择元素

java - Maven中parent标签的含义

Java Spring MongoDB @TextIndexed 注释未在现有数据库中创建索引