Spring与Struts整合

一 概述

1.整合目的

有了Spring以后,所有对象的创建任务都应该交给Spring容器来完成,这样做不仅是为了降低代码的耦合度,而且可以利用Spring容器作为代理工厂实现代理。

2.整合目标

将Spring容器中的bean注入Action中,将Action的创建与管理工作交给Spring容器。

二 实现

1.基础

Spring与Struts的整合建立在Spring与Web整合的基础之上。

2.整合架包

Struts2提供了与Spring兼容的架包struts2-spring-plugin.jar,整合需要导入该架包。

3.Spring创建Action

完成了Spring与Web的整合以后,Spring容器会根据名称将bean注入到action中,这一步完成了将Spring容器中的bean
注入到action中的任务,将action创建与管理工作交给Spring容器的任务还没有完成,在Spring配置文件的编写:

<bean id="springActionName"class="xxxxAction"scope="prototype">
     <property name="" ref="">//注入action中引用的 bean
</bean>

action是多例的,必须将作用域设为prototype.

4.Struts引用

<package name=""extends="struts-default"namespace="">
   <action name="myAction"class="springActionName">
      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   </action>
</package>

在Struts.xml中通过id引用在Spring容器中创建的Action对象。

原文地址:https://www.cnblogs.com/tonghun/p/7231220.html