Spring整合Strust2-【大饼】

来源:互联网 发布:锐捷网络 上海华讯 编辑:程序博客网 时间:2024/04/28 06:40

Spring-整合Struts2

今天看了看Spring与Strust2整合, 写了一点小总结,希望对大家有帮助,本人也是刚开始学Spring,如果不到之处还望大家多多指导和指正,如果有问题QQ联系 1281027677, 转载请指明作者!分享是一种美德!大笑


前言:

Struts2以后,提供了struts2-spring-plugin-*.jar来完成与Spring的整合;

一.需要的jar文件:

 

 SpringStruts2框架本身需要的jar文件以及他们所依赖的jar文件,比如  

 commons-logging.jar等等, 另外还需要Struts2发布包中的struts2-spring-plugin-xxx.jar

二.编写web.xml,配置Spring监听器:

     代码如下:

  

      <!-- 配置监听器 用来加载Spring配置文件  -->

     <listener>

      <!--  

           ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml  

           配置文件

       -->

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

    </listener>

      <!-- 

             context-param:上下文参数(当前整个应用的参数),Spring监听器启动的时候会去

             读取这个参数  

      -->

       <context-param>

            <!-- context的配置位置 -->

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

    

           <!-- 可以指定多个Spring配置文件的位置  -->

           <param-value>

                   /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml

           </param-value> --> 

      </context-param>

       解释:

       1.由于要引入Spring来管理Bean(Action),这时候就要使用ContextLoaderListener

       2. web.xml配置:

         <listener-class>

               org.springframework.web.context.ContextLoaderListener

         </listener-class>

          以上这句代码意思:一旦当前我们的Application(web应用程序)启动,

          ContextLoaderListener类就会

      <listener>            <listener-class>           org.springframework.web.context.ContextLoaderListener        </listener-class>      </listener>      <context-param>     <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml</param-value>       </context-param>

启动,而ContextLoaderListener类启动了以后就会去

          找Spring配置文件,就会把Spring配置文件中的Bean全部初始化,Spring容器  就产生了;

       3. classpath*:applicationContext.xml src根目录下查找Spring配置文件;

  

       4. 如果Spring配置文件被命名为applicationContext.xml,并且放在WEB-INF目录

         下,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF

         目录下寻找applicationContext.xml配置文件, 若存在多个Spring配置文件,则  在<param-value>中依次列出,之间以逗号隔开;

    完整代码:

       

 三. SpringStruts2整合,整合有两种方法,分别如下:

     

1. 第一种实现方法

   (1)Spring配置文件:

      将StrutsAction配置在Spring的配置文件中,同时将Action中引用的业务逻辑层中的对象一并注入,注意:在Spring配置文件中必须将ActionBean配置为 scope=prototype

      实例如下:

          <bean id="userAction" class="com.action.UserAction"  scope="prototype">

               <property name="userManager">

                  <ref bean="userManager"/>

              </property>

          </bean>

      为什么设置ActionBean配置scope=prototype?

      解释

          SpringBean的作用域(scope属性默认为singleton(单例)

              scope=prototype:所有对这个Bean的请求都会返回这个Bean的唯一实例;scope=prototype:每次请求这个Bean都会创建一个新的实例;

              如果我们不设置Bean中的scope属性,那么Action这个Bean的实例就是一   个单例,这样就会造成线程不安全,所以必须设置scope=prototype

       

     (2)Stuts2配置文件:

         在struts.xml中配置Action,指定<action>class属性为Spring配置文件中相    应Beanid或者name属值值。实例如下:

         <action name="users" class="userAction">

                <result name="success">/registerSuccess.jsp</result>

               <result name="fail">/registerFail.jsp</result>

         </action>

2.第二种实现方法:

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

    strust.xml实例如下:

    <action name="users" class="com.action.UserAction">

       <result name="success">/registerSuccess.jsp</result>

       <result name="fail">/registerFail.jsp</result>

   </action>

    

      (2)Action类中引用的业务逻辑层对象不需要自己去初始化,strust2Spring插件会       使用Bean的自动装配将业务逻辑层对象注入进来,其实Action类也不是Struts2 

           创建的,而是Struts2Spring插件创建的。默认情况下,Strust2Spring插件使   

           用byName的方式自动装配可以在struts.xml中通过增加Struts2常量来修改匹配

          方式,设置方式为:struts.objectFactory.spring.autoWire=name,可选的装配参数如:

        (a) name:等价于Spring配置中的autowire=”byName”,这时默认值;

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

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

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

       注意:

       使用第二种实现方式时,每次访问Action时,也都会创建一个新的Action实例;

3.至此,完成了两种方式的整合。比较这两种整合方式,其本质是一样的。不同之处在

     于,使用第二种自动装配方式时,由于没有在Spring配置文件中配置Action类,所以  需要对Action配置一些AOP之类的 内容时就很难实现了。


谢谢大家观看,如果不到之处还请批评指正!----大饼