spring与struts2整合方式
来源:互联网 发布:linux 改名字命令 编辑:程序博客网 时间:2024/06/06 09:02
有两种整合方式:
(1) 把Action配置在beans.xml里,利用Spring初始化Action的bean:
将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>
(2) 不需要在beans.xml里配置Action,利用Struts-Spring_Plugin插件自动初始化Action:
业务类在Spring配置文件中配置,Action不需要配置,Struts2的Action像没有整合Spring之前一样配置,<action>的class属性指定业务逻辑控制器类的全限定名。
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"
这里说了配置部分,但是,这里有一个问题, 就是Spring管理Action,如果按照第一方式,那么只要通过scope="property"来配置为每个请求创建一个Action实例。 那么第二种方式,并没有指定Action的作用域。
action不用在Spring中配置,仍按照没整合之前配置
(1) 把Action配置在beans.xml里,利用Spring初始化Action的bean:
将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>
(2) 不需要在beans.xml里配置Action,利用Struts-Spring_Plugin插件自动初始化Action:
业务类在Spring配置文件中配置,Action不需要配置,Struts2的Action像没有整合Spring之前一样配置,<action>的class属性指定业务逻辑控制器类的全限定名。
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"
这里说了配置部分,但是,这里有一个问题, 就是Spring管理Action,如果按照第一方式,那么只要通过scope="property"来配置为每个请求创建一个Action实例。 那么第二种方式,并没有指定Action的作用域。
<!-- 在Service层注入DAO --><bean id="bookService" class="com.service.BookService"><property name="bookDAO" ref="bookDAO"></property></bean>这里有点要注意,业务层对象bookService要提供setter方法,不然还是注入不了,没写set会导致空指针异常。
//注入service,导入插件包就开户了按名字自动注入private BookService bookService;public void setBookService(BookService bookService) {this.bookService = bookService;}
action不用在Spring中配置,仍按照没整合之前配置
<struts> <package name="default" namespace="/" extends="struts-default"><!-- 第一种 将Action 交给Spring管理 --><!--bookaddAction并不是真实类名,只是指向Spring管理Action Bean的id --><action name="bookadd" class="bookaddAction"></action>
<!-- 第二种 struts2 整合spring 方式, struts2自己管理Action,利用插件包实现 --><!-- <action name="bookadd" class="com.action.BookAction"></action> --> </package></struts>第一种方式时applicationContext.xml的配置,与上面action在struts.xml中配置的class属性一样<!-- action是多实例的,scope须改成prototype,切记 --><bean id="bookaddAction" class="com.action.BookAction" scope="prototype"><property name="bookService" ref="bookService"></property></bean>
阅读全文
0 0
- spring与struts2整合方式
- Struts2 与 Spring 整合
- Struts2与Spring整合
- Struts2与Spring整合
- spring与struts2整合
- spring与struts2整合
- struts2与spring整合
- Struts2与Spring整合
- Spring与Struts2整合
- struts2与spring整合
- struts2 与spring整合
- Struts2与Spring整合
- Struts2与Spring整合
- Spring与Struts2整合
- Struts2与Spring整合
- Spring与Struts2整合
- Struts2与Spring整合
- spring与struts2整合
- SpringMVC初识
- 三种列表标签的使用
- Go编程基础—常见结构
- hausdorff distance 豪斯多夫距离
- Git最常用功能,这一篇就够了!(结合开发场景)
- spring与struts2整合方式
- 动态规划之最长公共子序列问题(LCS)
- Linux中的cron和crontab
- 解决recyclerView.setOnScrollChangeListener只能只能在api23以上使用问题
- 详解TCP协议中控制位及URG&PSH的区别
- 架构师之路(十六)
- (转)每个人都应该读一读贝索斯的致股东信 1997-2016
- 对所用对象的通用方法
- jzoj P2153【2017.7.7普及】串