来自公众号的SPRING(11)

来源:互联网 发布:免费搜狐会员软件 编辑:程序博客网 时间:2024/06/07 21:13
Java程序员从笨鸟到菜鸟之(八十二)细谈Spring(十一)深入理解spring+struts2整合(附源码)                    
2016-09-08曹胜欢java那些事java那些事
java那些事

csh624366188

分享java开发中常用的技术,分享软件开发中各种新技术的应用方法。每天推送java技术相关或者互联网相关文章。关注“java那些事”,让自己做一个潮流的java技术人!《java程序员由笨鸟到菜鸟》系列文章火热更新中。

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


      我们一起来想想,如果让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 plain copy print?

  1. <listener> 

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

  3. <!-- default: /WEB-INF/applicationContext.xml --> 

  4. </listener> 




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


[html] view plain copy print?

  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 plain copy print?

  1. <struts> 

  2.     <beantype="com.opensymphony.xwork2.ObjectFactory"name="spring"class="org.apache.struts2.spring.StrutsSpringObjectFactory"/> 

  3.      

  4.     <!--  Make the Spring object factory the automatic default --> 

  5.     <constantname="struts.objectFactory"value="spring"/> 

  6.     <packagename="spring-default"> 

  7.         <interceptors> 

  8.             <interceptorname="autowiring"class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/> 

  9.             <interceptorname="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

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



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

[java] view plain copy print?

  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. publicclass StudentActionextends ActionSupport { 

  7. private Student student; 

  8. private StudentService service; 

  9. @Override 

  10. public String execute()throws Exception { 

  11. // TODO Auto-generated method stub 

  12. service.login(student); 

  13. return SUCCESS; 

  14. public StudentService getService() { 

  15. return service; 

  16. public Student getStudent() { 

  17. return student; 

  18. publicvoid setStudent(Student student) { 

  19. this.student = student; 

  20. publicvoid setService(StudentService service) { 

  21. this.service = service; 

  22. //public static void main(String[] args) { 

  23. //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 

  24. //"beans.xml"); 

  25. // 

  26. //StudentAction action = (StudentAction) context.getBean("StudentAction"); 

  27. // 

  28. //System.out.println(action == null); 

  29. // 

  30. //StudentService ser = action.getService(); 

  31. // 

  32. //ser.login(new Student("ss", "ss")); 

  33. // 

  34. //} 




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


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

Struts.xml:


[html] view plain copy print?

  1. <struts> 

  2. <packagename="registration"extends="struts-default"> 

  3. <actionname="StudentAction"class="com.bzu.action.StudentAction"> 

  4. <resultname="success">/Success.jsp</result> 

  5. <resultname="fail">/Fail.jsp</result> 

  6. </action> 

  7. </package> 

  8. </struts> 




Beans.xml

[html] view plain copy print?

  1. <beanid="stuDao"class="com.bzu.dao.impl.StudentDaoImpl"></bean> 

  2. <beanid="service"class="com.bzu.service.impl.StudentServiceImpl"> 

  3. <propertyname="dao"ref="stuDao"></property> 

  4. </bean> 

  5. <!--这里完全可以不写--> 

  6. <!-- 

  7. <beanid="StudentAction"class="com.bzu.action.StudentAction"scope="prototype"> 

  8. <propertyname="service"ref="service"></property> 

  9. </bean> 

  10. --> 






      上面的示例是用的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

[html] view plain copy print?

  1. <struts> 

  2. <packagename="registration"extends="struts-default"> 

  3. <actionname="StudentAction" class="stuAction"> 

  4. <resultname="success">/Success.jsp</result> 

  5. <resultname="fail">/Fail.jsp</result> 

  6. </action> 

  7. </package> 

  8. </struts> 




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

Beans.xml


[html] view plain copy print?

  1. <prename="code"class="html"><beanid="stuDao"class="com.bzu.dao.impl.StudentDaoImpl"></bean> 

  2. <beanid="service"class="com.bzu.service.impl.StudentServiceImpl"> 

  3. <propertyname="dao"ref="stuDao"></property> 

  4. </bean> 

  5. <!--现在这里必须写了,不写会报错--> 

  6. <beanid="stuAction"class="com.bzu.action.StudentAction"scope="prototype"> 

  7. <propertyname="service"ref="service"></property> 

  8. </bean></pre><br> 

  9. <br> 

  10. <pre></pre> 

  11. <p></p> 

  12. <pre></pre> 

  13. <p></p> 

  14. <p></p> 

  15. <p></p> 

  16. <p><spanstyle="font-size:18px">         OK<spanstyle="font-family:宋体"></span><spanstyle="font-family:Courier New">struts2+spring</span><spanstyle="font-family:宋体">整合讲到这就基本完了,当然我说的也不是很全面,希望大家见谅,希望大家能提出宝贵意见</span></span></p> 

  17. <p></p> 

  18. <pre></pre> 

  19. <pre></pre> 

  20.     

0 0