struts2和spring的整合


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

整合方式一app

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

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


(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>
xml

接着,在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>
作用域


整合方式二get

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

 

业务类在Spring配置文件中配置,Action不须要配置,Struts2Action像没有整合Spring以前同样配置,<action>class属性指定业务逻辑控制器类的全限定名。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的做用域。

相关文章
相关标签/搜索