Struts1.X與Spring集成——另一種方案

版權聲明:本文爲博主原創文章。未經博主贊成不得轉載。

https://blog.csdn.net/hanxuemin12345/article/details/38070063 css

上篇博客介紹了Struts1.XSpring集成的一種方案。html

Struts1.X與Spring集成——第一種方案java

此篇博客還以上篇博客的登陸樣例爲例,介紹Struts1.XSpring集成的還有一種方案。node

1,第一種方案 原理

回顧第一種方案集成原理:在Action中取得BeanFactory,經過BeanFactory取得業務邏輯對象web

此種方案的缺點:從嚴格意義的分層上來看,Action上看到了Spring的相關東西。依賴Spring API去查找東西。發生了依賴查找。因爲要查找依賴對象,因此要依賴Spring服務才幹找到,因爲在Spring提供的工廠裏。spring

應該是Action中看不到Spring相關東西。Action中看到的就是業務接口,這種話層次更加分明。apache

2。另一種方案 原理

基於第一種方案的缺點。咱們改爲不在去查找業務對象。讓IOC注入進來,僅僅要提供setter方法,就能把對象主動傳過來——依賴注入(侷限性:在同一個JVM裏可以,跨JVM不可以依賴注入)app

 

假設想被Spring注入,Action需要被Spring管理。也就是說LoginAction的建立是從IOC容器中建立出來的。而不該該再是讓Struts建立。假設仍是Struts建立的話,UserManager是不可能注入進來的。jsp

僅僅有從IOC容器中拿出來的東西,所有的依賴關係才被注入,因此Action必須被Spring管理。被Spring管理,就需要配置到Spring的配置文件裏。post

 

applicationContext-beans.xml


 

applicationContext-action.xml:


 

僅僅要經過BeanFactory getBean()"LoginAction"拿到,同一時候userManager的依賴關係都會注入進去;

 3,分析另一種方案提出的起因

第一種方案:


 

Webclient發出請求,請求ActionServletStruts流程是在Struts裏面要查看Struts-config.xml,LoginAction拿出來new。例如如下:


Struts進行new,僅僅會new LoginAction。其它的UserManager不會new,也不會注入。

因此此種流程是不行的,依賴對象不能注入。

但是,用Struts的話必須得請求Action。因此需要使用SpringSpring實現了一個ActionAction代理類:ActionProxy)——不會改變原先Action的相關接口,僅僅是代理,代理可以控制原目標。在調目標以前可以作些事情。因此Struts-config.xml文件裏就不該該配置LoginAction.java這個類了,要配置Spring給提供的一個類,它提供的類起到了什麼做用:webclient發出請求,需要StrutsSpring提供的代理類new出。這個代理類中負責拿到BeanFactory,經過getBean()。把此次請求相應的Action拿出來(是從IOC容器中拿出LoginAction),那麼它的依賴對象就都會被注入。

例如如下圖另一種方案:

另一種方案:


將第一種方案,Struts-config.xml中配置的LoginAction.Java(例如如下圖(1))改成配置org.springframework.web.struts.DelegatingActionProxy.class(2)

(1)


(2)


 

(3)

applicationContext-action.xml:配置LoginAction類,來管理LoginAction


 

一個請求過來,Strutsnew出代理類DelegatingActionProxy,經過代理類調用getBean(beanName,Action.Class)方法,將path的名稱"/login"傳入,到IOC中找到相應nameAction(即,LoginAction(applicationContext-action.xml中配置過了,如圖(3))

 

另一種方案集成原理:StrutsAction交給Spring建立。

將業務邏輯對象經過spring注入到Action中,從而避免了在Action類中的直接代碼查詢

(client請求---->代理action--->取得BeanFactory--->getBean(..)建立action演示樣例--->執行exctute方法)

4。代碼演示樣例

簡單登陸樣例

僅僅是Spring配置文件和Struts配置文件。以及LoginAction.java有所變更,其它代碼參考 Struts1.X與Spring集成——第一種方案

struts-config.xml配置文件:配置代理Action及ActionForm

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<!--生命ActionForm(即,告訴Struts哪一個是ActionForm)  -->
    <form-beans>
       <form-bean name="loginForm" type="com.bjpowernode.usermgr.web.forms.loginActionForm"/>
    </form-beans>
 
<!-- 描寫敘述Action -->
   <!-- 描寫敘述Action -->
    <action-mappings>
        <action path="/login"
                type="org.springframework.web.struts.DelegatingActionProxy"
                name="loginForm"
                scope="request">   
                
        <!-- 轉向信息 -->       
        <forward name="success" path="/login_success.jsp"/>
        </action>
    </action-mappings>
    <message-resources parameter="MessageResources" />
</struts-config>


applicationContext-beans.xml配置文件:配置業務邏輯對象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<!-- 這個IOC容器核心是個工廠,抽象工廠 -->
	<bean id="userManager" class="com.bjpowernode.usermgr.manager.UserMangerImpl"/>
    
</beans>

applicationContext-action.xml配置文件:配置Struts的Action

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<!-- 配置Struts的Action -->
	<bean name="/login" class="com.bjpowernode.usermgr.web.actions.LoginAction" scope="prototype">
       <property name="userManager" ref="userManager"/>
   </bean>
</beans>

5,執行

參考 Struts1.X與Spring集成——第一種方案


6,總結

第一種方案原理:Action中取得BeanFactory,經過BeanFactory取得業務邏輯對象——依賴查找;
另一種方案原理:將Struts的Action交給Spring建立,這樣業務邏輯對象將會被注入——依賴注入;

二者差異及優缺點:
第一種是依賴查找因爲要查找依賴對象,因此要依賴Spring服務才幹找到。這樣就依賴了Spring服務。假設脫離了Spring,Action對象不能單獨使用。
另一種是依賴注入,不依賴於別的服務。這樣就避免了依賴查找,不用關係其它服務是怎樣工做的,沒有侵入性,從而也使代碼很是清晰。
相關文章
相關標籤/搜索