struts2和spring的两种整合方式

struts2和spring的两种整合方式

首先,来看看如何让Spring 来管理Action.html


在struts.xml中加入spring

<constant name="struts.objectFactory" value="spring" />jsp


有两种整合方式:
(1) 把Action配置在beans.xml里,利用Spring初始化Action的beanspa

将Struts的业务逻辑控制器类配置在Spring的配置文件中,Action中引用的业务类一并注入。 
(这样的处理,必须将action类的scope配置成property)
Xml代码
<bean id="LoginAction" class="yaso.struts.action.LoginAction">
       <property name="loginDao" ref="LoginDao"/>
</bean>插件

接着,在struts.xml或者等效的Struts2配置文件中配置Action时,指定<action>的class属性为Spring配置文件中相应bean的id或者name值。示例以下: 
Xml代码
<action name=”LoginAction” class=”LoginAction>
       <result name=”success>/index.jsp</result>
</action>xml


 (2) 不须要在beans.xml里配置Action,利用Struts-Spring_Plugin插件自动初始化Actionhtm

业务类在Spring配置文件中配置,Action不须要配置,Struts2的Action像没有整合Spring以前同样配置,<action>的class属性指定业务逻辑控制器类的全限定名。blog

Action中引用的业务类不须要本身去初始化,Struts2的Spring插件会使用bean的自动装配将业务类注入进来,其实Action也不是Struts2建立的,而是Struts2的Spring插件建立的。默认状况下,插件使用by name的方式装配,能够经过增长Struts2常量来修改匹配方式:设置方式为:struts.objectFactory.spring.autoWire = typeName,可选的装配参数以下:作用域


 name:至关于spring配置的autowrie="byName"(默认)
 type:至关于spring配置的autowrie="byType"
 auto:至关于spring 配置的autowrie="autodetect"
 constructor: 至关于spring配置的autowrie="constructor"get

OK,这里说了配置部分,可是,这里有一个问题, 就是Spring管理Action,若是按照第一方式,那么只要经过scope="property"来配置为每一个请求建立一个Action实例。 那么第二种方式,咱们并无指定Action的做用域。

(好似也没有地方可配……),那么,这样的整合方式,Action的建立究竟是单例仍是多例的呢?  答案:也是每一个请求一个实例.

相关文章
相关标签/搜索