Spring与Struts2整合原理

来源:互联网 发布:手机ip代理软件 编辑:程序博客网 时间:2024/04/27 17:22

Spring 和Struts 2都是应用非常广泛的J2EE应用程序框架,Struts 2主要注重的是将视图层和控制层分开,但是不涉及对模型层的优化设计;而Spring除了实现Struts 2的MVC功能外,还可以利用其控制反转的思想实现对模型层的优化,从更深层次去降低应用程序各个组件的耦合程度。

1、添加ContextLoaderListener到web.xml 

Xml代码  收藏代码
  1. <listener>  
  2.     <listener-class>  
  3.             org.springframework.web.context.ContextLoaderListener  
  4.         </listener-class>  
  5. </listener>  
如果需要添加多个spring配置文件就要在web.xml加入contextConfigLocation这个context参数 

Xml代码  收藏代码
  1. <context-param>   
  2.         <param-name>contextConfigLocation</param-name>   
  3.         <param-value>   
  4.             /WEB-INF/classes/applicationContext-*.xml    
  5.         </param-value>   
  6.     </context-param>   
  7. <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>  
  10.            classpath:beans.xml  
  11.         </param-value>  
  12.     </context-param>  

ContextLoaderListener的作用 :

ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。 
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

(org.springframework.web.context.ContextLoaderListener创建出 WebApplicationContext容器对象, 并将创建出来的WebApplicationContext对象存储进了Web应用程序的application作用域中,存储时的key为 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个常量表示的字符串,以后在Web应用程序中就可以使用

application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) ;或

Spring提供的工具方法 WebApplicationContextUtils.getWebApplicationContext(application)来获得 spring容器对象

<context-param>(/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml)

<context-param>节点就是用来为这个全局对象配置参数的,这样就可以在整个项目中都能使用,spring就是通过servletConfig来读取web.xml中的路径参数来过得beans.xml等一些spring的xml文件的。用来定位Spring XML文件的上下文配置。ContextLoaderListener使用contextConfigLocation这个ServletContext初始化参数来指定WebApplicationContext容器对象的配置文件,如果没有配置contextConfigLocation这个 ServletContext的初始化参数,ContextLoaderListener则默认使用/WEB-INF /applicationContext.xml作为配置文件。

以上是web.xml,下面是修改struts.xml

Struts 2在发布的时候在其插件包struts-2.1.2\lib中有struts2-spring-plugin-2.1.2.jar,正是它实现了Struts 2和Spring的整合。这个插件覆盖了Struts 2的ObjectFactory,所以在Struts 2创建一个对象的时候,例如Action类,它会先到Struts 2的配置文件去寻找类的名字,然后转到Spring配置文件中去查找名字找到该类。

当创建一个对象的时候,它会用Struts2 配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts 2框架自身创建 ,然后由Spring来装配 。Spring插件具体有如下几个作用:

— 允许Spring创建Action、Interceptror和Result。

— 由Struts创建的对象能够被Spring装配。

— 如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配action 。

在这个插件包中有个struts-plugin.xml文件,它的内容如下:

<struts>    <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />        <!--  Make the Spring object factory the automatic default -->    <constant name="struts.objectFactory" value="spring" />    <constant name="struts.class.reloading.watchList" value="" />    <constant name="struts.class.reloading.acceptClasses" value="" />    <constant name="struts.class.reloading.reloadConfig" value="false" />    <package name="spring-default">        <interceptors>            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>        </interceptors>    </package>    </struts>

其中设置了Struts 2框架常量struts.objectFactory的值为spring,默认情况下action是有struts2的工厂生产的.在这里把它交给了Spring生产

这里它将框架常量struts.objectFactory覆盖了,设置为”spring”,其实这里是使用了缩写,我们可以写全称:org.apache.struts2.spring.StrutsSpringObjectFactory。这个缩写的”spring”是和bean配置中的name属性相对应的。默认情况下所有由框架创建的对象都是由ObjectFactory实例化的ObjectFactory提供了与其它IoC容器如SpringPico等集成的方法。覆盖这个ObjectFactory的类必须继承ObjectFactory类或者它的任何子类,并且要带有一个不带参数的构造方法。在这里我们用org.apache.struts2.spring.StrutsSpringObjectFactory代替了默认的ObjectFactory

如果action不是使用Spring ObjectFactory创建的话,插件提供了两个拦截器来自动装配action,默认情况下框架使用的自动装配策略是name,也就是说框架会去Spring中寻找与action属性名字相同的bean,可选的装配策略还有:typeautoconstructor,我们可以通过常量struts.objectFactory.spring.autoWire来进行设置。

Struts 2框架整合Spring后,处理用户请求的Action并不是Struts框架创建的,而是由Spring插件创建的。创建实例时,不是利用配置Action时指定的class属性值,根据bean的配置id属性,从Spring容器中获得相应的实例。

注意:strut.xml中配置的actionclass值,应该与spring中配置的Action Bean id 相应。

<filter><filter-name>f</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>f</filter-name><url-pattern>/*</url-pattern></filter-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>










原创粉丝点击