struts2与spring整合时需要注意的点

      首先我们需要明白spring整合struts2中的什么东西,spring中的核心就是IOC和AOP,IOC是对象的容器,AOP是处理动态代理的;比如spring与hibernate整合时就要用到aop,具体就是把事务的开启与关闭交于spring中的aop处理.一句话,spring就是整合struts2的对象.

     先前struts.xml配置文件中action标签中的class属性的值是"包名+类名",这样配置就可以让struts2自己生成对象,但这样不符合模式设计六大原则,为了解决这个问题,我们就把生成对象交于spring来处理,它的配置是在struts.xml配置文件中添加<constant name="struts.objectFactory" value="spring"/>这个标签,这个标签的作用就是说明struts的对象生成交于spring处理,同时还要修改struts.xml配置文件中action标签中的class属性的值为 spring配置文件中的bean的id值.

     这时我们还要注意对象是在什么时候生成的问题,我们一般是让对象在服务器启动时就生成(或者说是初始化容器),这时就要在web.xml配置文件中加

                 <listener>
                         <!-- 此监听器默认读WEB-INF 下的applicationContext.xml 文件 启动时加载我们的spring ioc 容器 但不能读其他文件,所以需要再加一个配置 

                         加<context-param>标签-->

                         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                </listener>
      <!-- context-param 元素由 ServletContext对象解析 意思是读取src下 以applicationContext开头的所有文件 -->

               <!--   加<context-param>标签的作用就是读取src下所有以applicationContext 开头的文件   -->
      <context-param>
            <param-name>contextConfigLocation</param-name>
                      <param-value>classpath:applicationContext*.xml</param-value>
                </context-param>

           struts2与spring的整合就需要注意以上即可

  

原文地址:https://www.cnblogs.com/hwgok/p/5335476.html