java - Struts2中如何获取Springs ApplicationContext?

标签 java spring dependency-injection struts2 classpath

我有一个带有 Spring 插件的 struts2 Web 项目。 applicationContext.xml 位于

/WEB-INF/applicationContext.xml

我如何在我的类(class)中访问它

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

似乎不起作用。

最佳答案

您并没有真正以正确的方式处理此问题,因为您不应该加载应用程序上下文。但是,您尝试过吗:

new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

我建议您将以下内容添加到 web.xml 中:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这将确保 Spring IOC 容器在应用程序启动时初始化。然后,您可以使用 Spring 为您的操作提供连接的必要依赖项。您不需要像您正在做的那样获取 IOC 容器,因为这不是依赖项注入(inject)。

关于java - Struts2中如何获取Springs ApplicationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102554/

相关文章:

java - 使用 Apache 通用配置从字符串创建 JSON 配置

java - Spring Boot : How do you specify an environment variable that has dashes in the application. 属性?

javascript - AngularJs 中的服务注入(inject)

interface - 依赖注入(inject)和使用接口(interface)?

java - 用于部署 Web 应用程序的 Infinispan 独特缓存管理器

java - AngularJS + Spring : 415 unsupported media type

java - 防止连续出现相同的随机输出

Spring Data MongoDB 如何使用 javaconfig 样式设置自动连接重试 ="true"?

来自数据库的 Spring Boot 外部化配置

c# - 使用依赖注入(inject)将字典保存在应用程序中的位置