细谈Spring(十一)深刻理解spring+struts2整合(附源码)

  Springstruts2是咱们在项目架构中用的比较多的两个框架,怎么才能把这两个框架用好,怎么来整合是咱们掌握运用这两个框架的关键点,下面咱们就怎么来整合,从哪来整合,为何要整合,从这几点来看一下struts2spring整合。下面咱们来具体分析一下: html


      咱们一块儿来想一想,若是让springstruts2进行整合,咱们就但愿咱们能够在spring中直接注入actionspring容器初始化的时候就给咱们建好了action,可是事情不像咱们想象的那么简单,由于struts2action是由struts2本身new出来的,他不受spring的管理,因此没法自动注入。因此strutsspring整合的结合点在于,struts2action不能直接入service。好了,既然有了问题,spring或者struts2确定已经为咱们把这个问题解决了。struts2解决这个问题是他给咱们提供了一个struts-spring-plugin的插件,经过这个插件咱们就能够把咱们的struts2和是spring进行整合了。struts-spring-plugin将会对咱们的action进行管理,当spring须要action的时候他就能够向struts-spring-plugin来要了。 java


源码下载:用力点 node


下面咱们就具体的来看一下struts+spring整合过程 mysql


1.须要的jar包列表Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2


jar包名称 web

所在位置 spring

说明 sql

antlr-2.7.6.jar apache

hibernate/lib/required api

解析HQL 服务器

aspectjrt

spring/lib/aspectj

AOP

aspectjweaver

..

AOP

cglib-nodep-2.1_3.jar

spring/lib/cglib

代理,二进制加强

common-annotations.jar

spring/lib/j2ee

@Resource

commons-collections-3.1.jar

hibernate/lib/required

集合框架

commons-fileupload-1.2.1.jar

struts/lib

struts

commons-io-1.3.2

struts/lib

struts

commons-logging-1.1.1

单独下载,删除1.0.4(struts/lib)

struts

spring

dom4j-1.6.1.jar

hibernate/required

解析xml

ejb3-persistence

hibernate-annotation/lib

@Entity

freemarker-2.3.13

struts/lib

struts

hibernate3.jar

hibernate

hibernate-annotations

hibernate-annotation/

hibernate-common-annotations

hibernate-annotation/lib

javassist-3.9.0.GA.jar

hiberante/lib/required

hibernate

jta-1.1.jar

..

hibernate transaction

junit4.5

mysql-

ognl-2.6.11.jar

struts/lib

slf4j-api-1.5.8.jar

hibernate/lib/required

hibernate-log

slf4j-nop-1.5.8.jar

hibernate/lib/required

spring.jar

spring/dist

struts2-core-2.1.6.jar

struts/lib

xwork-2.1.2.jar

struts/lib

struts2

commons-dbcp

spring/lib/jarkata-commons

commons-pool.jar

..

struts2-spring-plugin-2.1.6.jar

struts/lib



2.配置好jar包之后,若是咱们想在服务器一启动就可让spring容器自动去加载咱们在配置文件中配置的bean,那么咱们要在web.xml中去配置一个监听器,这个监听器的做用是监听咱们的application,一旦咱们的项目启动就触发了监听器,咱们来看一下这个监听器的配置:


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- default: /WEB-INF/applicationContext.xml -->
</listener>


若是你的配置文件不想放在默认的位置,而是本身去指定位置,那么咱们将要在web.xml中再次配置以下:


<context-param>
<param-name>contextConfigLocation</param-name>
//这种配置能够指定多个配置文件,由于spring的配置文件能够分开写成好几个
<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
//指定spring配置文件的位置classpath下的beans.xml
<param-value>classpath:beans.xml</param-value>
</context-param>


      加载完上面的bean以后,咱们就要考虑去管理咱们的action了,咱们应该让spring去找struts去要相应的action,把action实例化为响应的bean,这时咱们就须要咱们上边所提到的struts-spring-plugin这个jar包了。加上这个jar包以后咱们就可让spring来管理咱们的action了。在struts-spring-plugin中有一个struts--pluginXml文件。下面咱们来看一下这个文件中的配置执行的具体过程


<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" />
    <package name="spring-default">
        <interceptors>
            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
            <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
        </interceptors>
    </package>    
</struts>


      关键是这个  <constant name="struts.objectFactory" value="spring" />这句配置就指明了咱们产生struts对象的工厂交给spring来产生咱们来看一下具体步骤:


struts2一块儿动就会去加载配置文件,其中包括strutspluginxml读取顺序:

struts的读常量:

struts-default.xml 

struts-plugin.xml

struts.xml

struts.properties

web.xml


       struts-plugin.xml指明了咱们产生对象的工厂交给spring来完成,执行到web.xml时,因为spring容器的监听器,这时spring容器就开始启动,spring启动以后会web.xml去找相应的配置,在web.xml中能够找到spring中的配置文件beans.xml,而后去初始化全部的bean

       spring去加载beans.xml的时候会自动把全部的bean初始化,而且放在本身的容器里。与此同时,struts2也有本身的bean容器,这个容器是strutsplugin提供的,他会把全部的action加载都加载到本身的容器里而后根据action的属性名字自动去spring去找名字和action属性相同的bean直接注入到action,也就是说。咱们在action中其实不用配置注入的东西,struts容器会自动给咱们注入。但仍是要提供相应的setget方法。而且名字要约定好,action属性和spring中的beanid要同样。但不要忘记,actionscope设置成prototype


下面咱们来看一下具体的示例代码:

package com.bzu.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bzu.entity.Student;
import com.bzu.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
private Student student;
private StudentService service;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
service.login(student);
return SUCCESS;
}
public StudentService getService() {
return service;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setService(StudentService service) {
this.service = service;
}
//public static void main(String[] args) {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
//"beans.xml");
//
//StudentAction action = (StudentAction) context.getBean("StudentAction");
//
//System.out.println(action == null);
//
//StudentService ser = action.getService();
//
//ser.login(new Student("ss", "ss"));
//
//}


      须要注意的是,springstruts2整合的时候有一个属性struts.objectFactory.spring.autoware,也就是说是struts属性的的自动装配类型,他的默认值是name,也就是说struts中的action中的属性不须要配置,他默认的去beans.xml中去找名字相同的,应该注意的是,在给一些属性起名字的时候不要和spring中配置的actionname属性相同,不然会报异常


下面咱们来看一下struts.xmlbeans.xml的相关配置:

Struts.xml:


<struts>
<package name="registration" extends="struts-default">
<action name="StudentAction" class="com.bzu.action.StudentAction">
<result name="success">/Success.jsp</result>
<result name="fail">/Fail.jsp</result>
</action>
</package>
</struts>


Beans.xml

<bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>
<bean id="service" class="com.bzu.service.impl.StudentServiceImpl">
<property name="dao" ref="stuDao"></property>
</bean>
<!--这里彻底能够不写-->
<!--
<bean id="StudentAction" class="com.bzu.action.StudentAction" scope="prototype">
<property name="service" ref="service"></property>
</bean>
-->


      上面的示例是用的struts2的容器来产生actionspring须要的时候要去struts容器里面去找action,而后再初始化bean,其实咱们还有一种方法产生action,那就是让spring容器去帮咱们来产生action,这样咱们产生action的时候就能够去spring容器里面去找了,具体应该是在spring配置文件beans.xml中把对应的action配置成bean,而后再struts.xml中配置action的时候,action对应的class就再也不是配置成该action对应的类了,而是配置这个action对应的spring容器的beanid属性,这样action初始化的时候就会去spring容器里面去找了。可是这样配置的话,咱们的action属性就必须配置了,由于spring来产生action后,struts容器就不会在自动去给咱们注入属性了。若是不配置属性的话会产生异常,下面咱们来看一下具体配置状况:

Action的代码仍是之前的代码,没有改变,这里就再也不重复写了,下面咱们来看一下struts.xml的配置:

Struts.xml

<struts>
<package name="registration" extends="struts-default">
<action name="StudentAction"  class="stuAction">
<result name="success">/Success.jsp</result>
<result name="fail">/Fail.jsp</result>
</action>
</package>
</struts>


上面的class对应的是下面actionbeanid属性

Beans.xml

<bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>
<bean id="service" class="com.bzu.service.impl.StudentServiceImpl">
<property name="dao" ref="stuDao"></property>
</bean>
<!--如今这里必须写了,不写会报错-->
<bean id="stuAction" class="com.bzu.action.StudentAction" scope="prototype">
<property name="service" ref="service"></property>
</bean>




         OKstruts2+spring整合讲到这就基本完了,固然我说的也不是很全面,但愿你们见谅,但愿你们能提出宝贵意见

相关文章
相关标签/搜索