Spring 如何在 Struts 动作中注入未在 applicationContext.xml 中定义的属性 [英] How Spring is injecting properties in Struts actions, that are not defined in applicationContext.xml

查看:27
本文介绍了Spring 如何在 Struts 动作中注入未在 applicationContext.xml 中定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用 Spring 3 + Struts 2 + Hibernate 3 等框架.它也使用 spring 安全性.

I am in a project having frameworks like Spring 3 + Struts 2 + Hibernate 3. Also it uses spring security.

Spring 和 Struts 是使用 web.xml

Spring and Struts is integrated using spring ContextLoaderListener defined in web.xml

<listener>
    <display-name>Spring Initialization</display-name>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

有几个服务类通常会调用数据库.像 agentserviceprocess 就是这样一个类,它具有像 getAgents() 这样的方法.

There are several service classes which usually make db calls. like agentserviceprocess is one such class having method like getAgents().

Struts 动作对每个进程都有一个属性和 setter getter,这些进程类在 application-context.xml 中定义为 bean.

Struts actions have a property and setter getter for each process and those process classes are defined as bean in application-context.xml.

令人惊讶的是,我找不到任何将动作类作为类参数的 bean.我的意思是 spring 没有控制动作对象的实例化.那么spring如何在这些action中注入服务属性对象呢?

Surprisingly I can not find any bean having action class as class parameter. I mean spring is not controlling the action object instantiation. Then how spring can inject service property objects inside these actions?

另外,我找到了以下条目

Also, I found following entry

<filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

推荐答案

要与 Spring 集成,您应该使用 struts2-spring 插件.

For integration with Spring you should use struts2-spring plugin.

当这个插件被加载时,插件的配置被应用到使用它自己的对象工厂来实例化动作类.

When this plugin is loaded the plugin's configuration is applied to use its own object factory to instantiate action classes.

当构建动作实例时,它会将它们传递给 Spring 进行自动装配.

When action instanses are built it passes them to Spring for autowiring.

您应该为自动装配提供属性的 getter 和 setter.您还为依赖项提供了 spring 配置.

You should provide getters and setters of properties for being autowired. You have also provide the spring configuration for dependencies.

这篇关于Spring 如何在 Struts 动作中注入未在 applicationContext.xml 中定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆