我的java天地

整合Struts2与Spring的步骤

一、 需要的 JAR 文件为: Spring Struts2 框架本身需要的 JAR 文件以及他们所依赖的 JAR 文件,比如 commons-logging.jar 等等,另外还需要 Struts2 发布包中的 struts2-spring-plugin-x.xx.jar

二、 web.xml 中增加 WebApplicationContext 的相应配置,以下两种配置方式本质是一样的。

1.         Servlet 2.3 及以上版本可以使用监听器,相应配置如下:

<context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

</context-param>

<listener>

              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

如果 spring 配置文件被命名为 applicationContext.xml ,并且放在 WEB-INF 目录下,则不需要配置 <context-param> ,因为 ContextLoaderListener 默认在 WEB-INF 目录下寻找名为 applicationContext.xml 的文件。若存在多个 Spring 配置文件,则在 <param-value> 中依次列出,之间以逗号隔开。

2.         Servlet 2.3 以下版本由于不支持 <listener> ,需要配置 <servlet> ,格式如下:

<context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

</context-param>

<servlet>

              <servlet-name>contextLoaderServlet</servlet-name>

              <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

              <load-on-startup>1</load-on-startup>

</servlet>

如果 spring 配置文件被命名为 applicationContext.xml ,并且放在 WEB-INF 目录下,则不需要配置 <context-param> ,因为 ContextLoaderListener 默认在 WEB-INF 目录下寻找名为 applicationContext.xml 的文件,或者是名字为 contextConfigLocation ServletContext 参数所指定的文件。由于该 Servlet 配置只是为了在容器启动时能启动 ContextLoaderServlet 使其工作,而不需要引用该 Servlet ,所以不需要配置 <servlet-mapping>

三、 web.xml 中完成加载 WebApplicationContext 之后,接下来就可以做到 Spring Struts2 的整合了。整合有两种方法,分别叙述如下:

1.         第一种实现方法:

1)        Struts 的业务逻辑控制器类配置在 Spring 的配置文件中,业务逻辑控制器中引用的业务类一并注入。注意,必须将业务逻辑控制器类配置为 scope=”prototype”
示例如下:

<bean id=”LoginAction” class=”yaso.struts.action.LoginAction”>

                     <property name=”loginDao” ref=”LoginDao”/>

</bean>

2)        struts.xml 或者等效的 Struts2 配置文件中配置 Action 时,指定 <action> class 属性为 Spring 配置文件中相应 bean id 或者 name 值。示例如下:

<action name=”LoginAction” class=”LoginAction”>

                     <result name=”success”>/index.jsp</result>

</action>

2.         第二种实现方法:

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

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

a)         name :等价于 Spring 配置中的 autowire=”byName” ,这是缺省值。

b)        type :等价于 Spring 配置中的 autowire=”byType”

c)         auto :等价于 Spring 配置中的 autowire=”autodetect”

d)        constructor :等价于 Spring 配置中的 autowire=” constructor”

四、 如果原先在 Struts2 中使用了多个 object factory ,则需要通过 Struts2 常量显式指定 object factory ,方式如下: struts.objectFactory = spring ;如果没有使用多个 object factory ,这一步可以省略。

五、 可以通过设增加 Struts2 常量来指定是否使用 Spring 自身的类缓存机制。可以设定的值为 true false ,默认为 true 。设置方式为: struts.objectFactory.spring.useClassCache = false

六、 至此,完成了两种方式的整合。比较这两种整合方式,其本质是一样的。不同之处在于,使用第二种自动装配的方式时,由于没有在 Spring 中配置业务逻辑控制器,所以需要对其配置一些 AOP 之类的内容时就很难实现了。

posted on 2009-03-19 23:14 tobyxiong 阅读(1758) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(3)

随笔分类(144)

随笔档案(157)

相册

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜