Spring整合Struts的兩種方式介紹

1 使用Spring託管Struts Action

該種方式就是將Struts Action也視爲一種Bean交給Spring來進行託管,使用時Struts的配置文件中配置的Action的classs屬性再也不是具體Action的實現類,而是在Spring配置文件中配置的BeanID,也就是說具體是Action實現類是在Spring的配置文件中進行配置的,Struts的配置文件中的Class屬性只是Spring文件中Bean的ID。Struts配置文件以下示例:web

<action name="LoginAction" class="loginAction">

       <!--登陸Action com.simpleworkflow.action.LoginAction-->

       <result name="input">/WEB-INF/content/login.jsp</result>

        <result name="mgr">/WEB-INF/content/manager/index.jsp</result>

        <result name="emp">/WEB-INF/content/employee/index.jsp</result>

        <result name="error">/WEB-INF/content/login.jsp</result>

</action>

 

同時須要在Struts配置文件中加入以下配置:spring

<struts>

    <constant name="struts.objectFactory" value="spring" />

</struts>

 

而Spring的配置文件中有以下配置:apache

<!--配置Action-->

<bean id="loginAction" class="com.simpleworkflow.action.LoginAction" scope="prototype">

    <!--登陸驗證Action-->

    <property name="empService" ref="employeeOperator"/>

    <property name="mgrService" ref="managerOperator"/>

</bean>

 

能夠看到,在Spring中配置的Bean的Class屬性纔是該Action的具體實現類,並且還能夠爲該Action設置其行爲,該行爲能夠有singleton、prototype、request、session、global Session幾個值(幾個值的詳細解釋請參考Spring Bean介紹),因爲Web環境下,Action一般是無狀態的,即每一個HttpRequest對應一個Action,請求完成後該Action就應該進行釋放,而不是繼續保留供其餘請求使用,所以,Action的Scope屬性的值一般設爲prototype,表示每次請求都會產生一個新的實例。session

須要注意的是,爲了完成Spring託管Struts Action,必須加入一個包,該包爲:struts2-spring-plugin-2.2.1.jar,或者其餘版本。若是系統中加入了該包,那麼就不須要在Struts配置文件中加入<constant/>配置了,該常量配置是指示Action的建立者由Struts變爲Spring。爲何加入了該包之後就不須要加入<constant/>的配置了呢?這是因爲在包內定義了一個struts-plugin.xml的文件,該文件內容以下所示:app

<struts>

     <!—建立Action的類變爲了StrutsSpringObjectFactory-->

    <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" />

    <constant name="struts.class.reloading.watchList" value="" />

    <constant name="struts.class.reloading.acceptClasses" value="" />

    <constant name="struts.class.reloading.reloadConfig" value="false" />

    <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>

 

因爲這種方式Struts配置文件中指定的Action的Class屬性並非真實的實現類,所以破壞了Struts配置文件的原生性,而且須要在Spring中添加Action的配置文件,形成大量的配置冗餘,所以更加推薦自動裝配的方式。框架

 

2 自動裝配方式(推薦)

由Spring託管Struts Action的方式(方式1)將Action的建立者由Struts改變爲Spring,Action再也不由Struts核心進行建立,而是相似應用中的其餘Bean同樣由Spring進行容器在初始化時進行建立。自動裝配則否則,它會用Struts2配置文件中的class屬性去和Spring配置文件中的id屬性進行關聯(存在一個尋找過程),若是能找到則由Spring建立,不然由Struts2框架自身建立,而後由Spring來裝配。因此Action的實際建立者仍然是Struts(沒人會拿包路徑當BeanID),Spring只是負責裝配。jsp

一樣須要引入上面介紹的包(必須引入該包了),而且在Struts.xml中加入以下常量屬性配置(必須):ide

<constant name="struts.objectFactory.spring.autoWire" value="true"></constant>

 

可是Struts配置文件中Action的Class屬性再也不是BeanID,而是實際的Action類。spa

<action name="loginAction" class="com.huateng.framework.action.LoginAction">

       <result name="emperorsuccess">/fpages/emperorMain.jsp</result>

       <result name="adminsuccess">/fpages/admin.jsp</result>

       <result name="queensuccess">/fpages/queenMain.jsp</result>

       <result name="ministersuccess">/fpages/ministerMain.jsp</result>

       <result name="input">/fpages/login.jsp</result>

</action>

 

而在Spring配置文件中不須要再次配置該Action。prototype

 

3   方式一、2的對比

方式一中加入額外插件,可加可不加常量配置語句,Action的建立者爲Spring,他會根據Action的Class屬性的值搜索Spring配置文件,找到Bean Id爲Class屬性值的Bean。Action由Spring在初始化時建立並完成後續的裝載(裝載:在Action中使用的其餘Bean由Spring容器生成,並注入到Action中去。詳見4.4)。因爲須要在兩個配置文件中添加配置,所以容易形成配置冗餘。

方式二加入了額外插件,以及在Struts配置文件中加入一條常量配置,Action的實際建立者仍爲Struts,Spring只是負責爲建立好的Action注入所須要的邏輯組件。

 

方式1

方式2

使用方法

加入插件;

加插件,加常量配置

Action的建立者

Spring

在容器中尋找,找到則爲Spring,未找到則爲Struts

插件中的組件裝配攔截器是否起做用

插件中的攔截器起做用。Spring負責注入邏輯組件。

插件中的攔截器起做用。Spring負責注入邏輯組件。

 

4 使用Spring管理Struts中使用到的Bean

使用自動裝配方式來整合Spring和Struts,Struts的配置文件—struts.xml中Action的定義方式不須要作任何改變,可是須要在web.xml中配置Spring的配置文件,來加載Bean的定義文件,加載後容器就會託管已經定義好的Bean,而在Action中,只須要定義須要注入的Bean,好比Service等,同時設置好Setter方法和Getter方法,在execute方法中直接使用該Bean的方法便可。

須要改變的地方有:

須要打開Spring託管Struts的開關,該開關至關因而告知Spring,要負責爲定義的Action注入所需的邏輯組件。在Struts.xml中添加以下語句(方式2須要加入,若是是方式一不須要):

<constant name="struts.objectFactory.spring.autoWire" value="true"></constant>

 

變量struts.objectFactory.spring.autoWire的值設置爲true即爲標識自動裝配打開,Spring容器會自動根據在Struts Action類中定義的Bean的名字查找容器中是否有該容器的定義,若是有,就自動爲該類(Action)注入該Bean,若是沒有那麼就沒法完成注入。

這種策略下,Struts的配置文件和不整合Spring時沒有區別(須要加入上面的語句)。區別在於若是是方式1,Action的建立者是Spring,那麼後續的裝配天然由Spring容器完成。而方式2因爲Action的實際建立者仍然是Struts,而Spring容器只是負責爲建立好的Action實例注入所須要的邏輯組件而已。

 

5     struts.objectFactory和struts.objectFactory.spring.autoWire

struts.objectFactory這個屬性用於說明Struts2的對象池建立工廠,Struts2也有本身的對象池,就像Spring那樣,在配置文件中你能夠引用對象池中的對象,你能夠藉助於Spring中的對象池,當想要獲得Spring中的對象池時,申明struts.objectFactory爲Spring的對象池構建工廠。當指定struts.objectFactory爲spring時,struts2框架就會把bean轉發給spring來建立,裝配,注入。可是bean建立完成以後,仍是由struts容器來管理其生命週期。

在struts.xml中的代碼以下:

<constant name="struts.objectFactory" value="spring" />

 

struts.objectFactory.spring.autoWire是用spring插件經過覆蓋(override)Struts2的 ObjectFactory來加強核心框架對象的建立。當建立一個對象的時候,它會用Struts2配置文件中的class屬性去和Spring配置文件中的id屬性進行關聯,若是能找到則由Spring建立,不然由Struts2框架自身建立,而後由Spring來裝配。

Spring插件(struts2-spring-plugin-2.2.1.jar)具體有以下幾個做用:

1. 容許spring來建立Action、Interceptror和Result;

2. 由Struts建立的對象可以被Spring裝配;

3. 提供了2個攔截器來自動裝配action;

這裏要注意的是,咱們沒必要在Spring中去註冊action,儘管咱們能夠這麼去作,一般Struts框架會自動的從action mapping中建立action對象。

這樣就是讓spring去管理這些bean

相關文章
相關標籤/搜索