Java程序員從笨鳥到菜鳥之(八十二)細談Spring(十一)深入理解spring+struts2整合(附源碼)

 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,一旦我們的項目啓動就觸發了監聽器,我們來看一下這個監聽器的配置:


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


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


  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文件。下面我們來看一下這個文件中的配置執行的具體過程:


  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

       spring去加載beans.xml的時候會自動把所有的bean初始化,並且放在自己的容器裏。與此同時,struts2也有自己的bean容器,這個容器是strutsplugin提供的,他會把所有的action加載都加載到自己的容器裏然後根據action的屬性名字自動去spring去找名字和action屬性相同的bean直接注入到action,也就是說。我們在action中其實不用配置注入的東西,struts容器會自動給我們注入。但還是要提供相應的setget方法。並且名字要約定好,action屬性和spring中的beanid要一樣。但不要忘記,actionscope設置成prototype


下面我們來看一下具體的示例代碼:

  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. @Override  
  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. }  
  24. public void setService(StudentService service) {  
  25. this.service = service;  
  26. }  
  27. //public static void main(String[] args) {  
  28. //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  29. //"beans.xml");  
  30. //  
  31. //StudentAction action = (StudentAction) context.getBean("StudentAction");  
  32. //  
  33. //System.out.println(action == null);  
  34. //  
  35. //StudentService ser = action.getService();  
  36. //  
  37. //ser.login(new Student("ss", "ss"));  
  38. //  
  39. //}  


      需要注意的是,springstruts2整合的時候有一個屬性struts.objectFactory.spring.autoware,也就是說是struts屬性的的自動裝配類型,他的默認值是name,也就是說struts中的action中的屬性不需要配置,他默認的去beans.xml中去找名字相同的,應該注意的是,在給一些屬性起名字的時候不要和spring中配置的actionname屬性相同,否則會報異常


下面我們來看一下struts.xmlbeans.xml的相關配置:

Struts.xml:


  1. <struts>  
  2. <package name="registration" extends="struts-default">  
  3. <action name="StudentAction" class="com.bzu.action.StudentAction">  
  4. <result name="success">/Success.jsp</result>  
  5. <result name="fail">/Fail.jsp</result>  
  6. </action>  
  7. </package>  
  8. </struts>  


Beans.xml

  1. <bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>  
  2. <bean id="service" class="com.bzu.service.impl.StudentServiceImpl">  
  3. <property name="dao" ref="stuDao"></property>  
  4. </bean>  
  5. <!--這裏完全可以不寫-->  
  6. <!--  
  7. <bean id="StudentAction" class="com.bzu.action.StudentAction" scope="prototype">  
  8. <property name="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

  1. <struts>  
  2. <package name="registration" extends="struts-default">  
  3. <action name="StudentAction"  class="stuAction">  
  4. <result name="success">/Success.jsp</result>  
  5. <result name="fail">/Fail.jsp</result>  
  6. </action>  
  7. </package>  
  8. </struts>  


上面的class對應的是下面actionbeanid屬性

Beans.xml

  1. <pre name="code" class="html"><bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>  
  2. <bean id="service" class="com.bzu.service.impl.StudentServiceImpl">  
  3. <property name="dao" ref="stuDao"></property>  
  4. </bean>  
  5. <!--現在這裏必須寫了,不寫會報錯-->  
  6. <bean id="stuAction" class="com.bzu.action.StudentAction" scope="prototype">  
  7. <property name="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><span style="font-size:18px">         OK<span style="font-family:宋體"></span><span style="font-family:Courier New">struts2+spring</span><span style="font-family:宋體">整合講到這就基本完了,當然我說的也不是很全面,希望大家見諒,希望大家能提出寶貴意見</span></span></p>  
  17. <p></p>  
  18. <pre></pre>  
  19. <pre></pre>  
  20.       
  21.         <div style="padding-top:20px">           
  22.             <p style="font-size:12px;">版權聲明:本文爲博主原創文章,未經博主允許不得轉載。</p>  
  23.         </div>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章