spring+struts2整合

来源:互联网 发布:手机商品期货交易软件 编辑:程序博客网 时间:2024/06/05 15:00

如果让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来要了。


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


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


jar包名称

所在位置

说明

antlr-2.7.6.jar

hibernate/lib/required

解析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,一旦我们的项目启动就触发了监听器,我们来看一下这个监听器的配置:


[html] view plaincopyprint?
  1. <listener>  
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. <!-- default: /WEB-INF/applicationContext.xml -->  
  4. </listener>  


如果你的配置文件不想放在默认的位置,而是自己去指定位置,那么我们将要在web.xml中再次配置如下:


[html] view plaincopyprint?
  1. <context-param>  
  2. <param-name>contextConfigLocation</param-name>  
  3. //这种配置可以指定多个配置文件,因为spring的配置文件可以分开写成好几个  
  4. <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->  
  5. //指定spring配置文件的位置classpath下的beans.xml  
  6. <param-value>classpath:beans.xml</param-value>  
  7. </context-param>  


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


[html] view plaincopyprint?
  1. <struts>  
  2.     <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />  
  3.       
  4.     <!--  Make the Spring object factory the automatic default -->  
  5.     <constant name="struts.objectFactory" value="spring" />  
  6.     <package name="spring-default">  
  7.         <interceptors>  
  8.             <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>  
  9.             <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>  
  10.         </interceptors>  
  11.     </package>      
  12. </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


管理Action的两种方式:


1. struts负责管理Action

       spring去加载beans.xml的时候会自动把所有的bean初始化,并且放在自己的容器里。与此同时,struts2也有自己的bean容器,这个容器是strutsplugin提供的,他会把所有的action加载都加载到自己的容器里然后根据action的属性名字自动去spring去找名字和action属性相同的bean直接注入到action,也就是说。我们在action中其实不用配置注入的东西,struts容器会帮我们自动把属性都注入。但还是要提供相应的setget方法。并且名字要约定好,action属性和spring中的beanid要一样。actionscope设置成prototype(默认也是prototype)。


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

struts.xml配置如下:

    <package name="student" namespace="/secure" extends="default">
        <action name="bar" class="com.bzu.action.StudentAction">
            <result>bar.ftl</result>
        </action>
    </package>

[java] view plaincopyprint?
  1. package com.bzu.action;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3. import com.bzu.entity.Student;  
  4. import com.bzu.service.StudentService;  
  5. import com.opensymphony.xwork2.ActionSupport;  
  6. public class StudentAction extends ActionSupport {  
  7. private Student student;  
  8. private StudentService service;  

  9. // 默认为prototype 
  10. public String execute() throws Exception {  
  11. // TODO Auto-generated method stub  
  12. service.login(student);  
  13. return SUCCESS;  
  14. }  
  15. public StudentService getService() {  
  16. return service;  
  17. }  
  18. public Student getStudent() {  
  19. return student;  
  20. }  
  21. public void setStudent(Student student) {  
  22. this.student = student;  
  23. // 这里的service,struts容器会自动帮我们在spring中查找并注入。不需要我们写@Resource指定 
  24. public void setService(StudentService service) {  
  25. this.service = service;  
  26. }  
  27. }

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


2.  spring负责产生并管理Action

struts.xml配置如下:

    <package name="secure" namespace="/secure" extends="default">
        <action name="bar" class="bar">
            <result>bar.ftl</result>
        </action>
    </package>


要是把struts中action的class设置为spring容器中的bean的id时,action的创建及属性注入就由spring来管理了。



原创粉丝点击