Spring整合Strust2

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类就会启动,而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>中依次列出,之间以逗号隔开;

   完整代码:

  

      <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>

. 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.  至此,完成了两种方式的整合。比较这两种整合方式,其本质是一样的。网上些人说这种方法是有区别的,说第二种方式无法使用AOP功能,但是自己写了一个小实验,得 

    出结论:“这两种方式都是Struts2将类的管理交给Spring,什么AOP、IOC功能,Spring完全可以按照自己的方式,想怎么使用就怎么使用,所不存在什么区别,要说区别我 

   感觉就是配置上的区别”。

谢谢大家阅读,如果不到之处还请批评指教---【大饼】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值