有了Spring之后,全部对象的建立任务都应该交给Spring容器来完成,这样作不只是为了下降代码的耦合度,并且能够利用Spring容器做为代理工厂实现代理。spring
将Spring容器中的bean注入Action中,将Action的建立与管理工做交给Spring容器。spa
Spring与Struts的整合创建在Spring与Web整合的基础之上。prototype
Struts2提供了与Spring兼容的架包struts2-spring-plugin.jar,整合须要导入该架包。代理
完成了Spring与Web的整合之后,Spring容器会根据名称将bean注入到action中,这一步完成了将Spring容器中的bean
注入到action中的任务,将action建立与管理工做交给Spring容器的任务尚未完成,在Spring配置文件的编写:code
<bean id="springActionName"class="xxxxAction"scope="prototype"> <property name="" ref="">//注入action中引用的 bean </bean>
action是多例的,必须将做用域设为prototype.xml
<package name=""extends="struts-default"namespace=""> <action name="myAction"class="springActionName"> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx </action> </package>
在Struts.xml中经过id引用在Spring容器中建立的Action对象。对象