struts2和spring的整合


struts2spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean有两种整合方式

整合方式一

(1) struts.xml中配置Struts2actionSpring来负责进行实例化 

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


(2) Action配置在applicationContext.xml里,利用Spring初始化Actionbean

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

接着,在struts.xml中配置Action时,指定<action>class属性为Spring配置文件中相应beanid或者name值访问该Action时,会通过class对应值去spring中寻找相同id值的bean 。示例如下: 
Xml代码
<action name=”LoginAction” class=”LoginAction”>
       <result name=”success”>/index.jsp</result>
</action>


整合方式二

不需要在applicationContext.xml.xml里配置Action,利用Struts-Spring_Plugin插件自动初始化Action

 

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

Action中引用的业务类不需要自己去初始化,Struts2Spring插件会使用bean的自动装配将业务类注入进来,其实Action也不是Struts2创建的,而是Struts2Spring插件创建的。默认情况下,插件使用by name的方式装配,可以通过增加Struts2常量来修改匹配方式:设置方式为:struts.objectFactory.spring.autoWire = typeName,可选的装配参数如下:


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

OK,这里说了配置部分,但是,这里有一个问题, 就是Spring管理Action,如果按照第一方式,那么只要通过scope="property"来配置为每个请求创建一个Action实例。 那么第二种方式,我们并没有指定Action的作用域。

 

转载于:https://my.oschina.net/lsl1991/blog/680583

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值