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

查看:225
本文介绍了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使用spring <$进行集成c $ c> ContextLoaderListener 在 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如何在这些操作中注入服务属性对象?

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插件。加载此插件时,将应用插件的配置以使用其自己的对象工厂来实例化操作类。当构建动作实例时,它将它们传递给spring以进行自动装配。您应该提供自动装配的属性的getter和setter。您还提供了依赖项的弹簧配置。

For integration with spring you should use struts2-spring plugin. When this plugin is loaded a plugin's configuration is applied to use its own object factory to instantiate action classes. When action instanses are built it passes them to spring for autowiring. 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天全站免登陆