struts2+spring的兩種整合方式

也許有些人會由於學習了struts1,會覺得struts二、struts1與spring的整合也是同樣的,其實這二者相差甚遠。下面就來說解一下struts2與spring的整合兩種方案。(部分轉載,裏面也有本身的內容)web

    藉助於Spring插件(Struts2-spring-plugin-XXX.jar),咱們能夠很是簡單地完成Spring和Struts2的整合,這種整合包括讓Action自動裝配Spring容器中的Bean,以及讓Spring管理應用中的Action兩種方式,無論採用哪一種方式,完成Struts2和Spring的整合都是很是簡單的,並且差異不大.一旦在Web應用中安裝了Spring插件,便可充分利用該插件提供的功能:spring

1,能夠經過Spring來建立全部的Action,Interceptor和Result.app

2,能夠在Struts建立了某個對象(Action實例)以後,Spring將其依賴的組件自動注入該對象框架

3,提供了兩個攔截器來完成自動裝配.jsp

此外,在使用Spring容器以前,必須先完成Spring容器的初始化,爲了完成Spring容器的初始化,Struts2利用了Spring所提供的兩種初始化方式.學習

1.  利用ContextLoaderListenerspa

Spring提供一個ContextLoaderListener對象,該類能夠做爲Web應用的Listener使用,它會在Web應用啓動時自動查找WEB-INF/下的applicationContext.xml配置文件(Spring的配置文件),而且根據該文件來建立Spring容器.所以,若是Web應用中只有一個Spring配置文件,而且文件名爲"applicationContext.xml",並將該文件放在Web應用的WEB-INF/路徑下,則只需在web.xml文件中增長以下一段便可:.net

<!-- 根據默認配置文件來初始化Spring容器 -->prototype

    <listener>插件

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

    </listener>

若是有多個配置文件須要載入,或則applicationContext.xml不在WEB-INF目錄下,則應該在web.xml中再使用<context-param>元素來肯定配置文件的文件名,內容以下:

<!-- 定義spring配置文件 -->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>

     /WEB-INF/applicationContext.xml,

     /WEB-INF/classes/conf/spring/applicationContext-*.xml

   </param-value>

</context-param>

 

若是spring配置文件被命名爲applicationContext.xml,而且放在WEB-INF目錄下,則不須要配置<context-param>,由於ContextLoaderListener默認在WEB-INF目錄下尋找名爲applicationContext.xml的文件。若存在多個Spring配置文件,則在<param-value>中依次列出,之間以逗號隔開。

 

 

2.  採用load-on-startup Servlet建立ApplicationContext(這也是爲了兼容Servlet 2.3如下版本,它們不支持<listener>,須要配置<servlet>)

利用Listener建立Spring容器很簡單,但有一個明顯的侷限,由於Listener是Servlet2.3之後纔開始出現的規範,這意味着在低版本的Servlet中只能利用Web應用中的load-on-startup的Servlet而不能利用Listener.這二者的做用是同樣的.爲了使用load-on-startup Servlet來建立Spring容器,Spring提供了一個特殊的Servlet類:ContextLoaderServlet,該Servlet在初始化時,會自動查找WEB-INF/下的"applicationContext.xml"文件,若是隻有一個配置文件,而且名爲"applicationContext.xml",則在web.xml文件中的配置以下:

<servlet>

<servlet-name>context</servlet-name>

<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

servlet僅用於提供後臺服務,負責建立spring容器,無需響應客戶端請求,所以無需配置servlet mapping.

若是有多個配置文件,則與上面同樣使用<context-param>元素來肯定多個配置文件,事實上,不管是ContextLoaderServlet仍是ContextLoaderListener都是經過調用ContextLoader來建立spring容器的。

 

Servlet規範中Listener老是比Servlet優先加載的,所以,採用ContextLoaderListener更好(web.xml 的加載順序是:context-param -> listener -> filter -> servlet ).

 

爲了讓控制器Action訪問Spring的業務邏輯組件,有兩種策略:

1.  Spring管理控制器,並利用依賴注入爲控制器注入業務邏輯組件

 

1)        將Struts的業務邏輯控制器類配置在Spring的配置文件中,業務邏輯控制器中引用的業務類一併注入。注意, 必須將業務邏輯控制器類配置爲scope=」prototype」! 示例以下:
<bean id=」LoginAction」 class=」yaso.struts.action.LoginAction」>
                     <property name=」loginDao」 ref=」LoginDao」/>
</bean>
2)        在struts.xml或者等效的Struts2配置文件中配置Action時,指定<action>的class屬性爲Spring配置文件中相應bean的id或者name值。示例以下:
<action name=」LoginAction」 class=」 LoginAction」>
                     <result name=」success」>/index.jsp</result>
</action>

 

 

2.  控制器定位Spring工廠,也就是Spring的容器,從Spring容器中取得所需的業務邏輯組件.

 

1)        業務類在Spring配置文件中配置,業務邏輯控制器類不須要配置,Struts2的Action像沒有整合Spring以前同樣配置,<action>的class屬性指定業務邏輯控制器類的全限定名。
2)        業務邏輯控制器類中引用的業務類不須要本身去初始化,Struts2的Spring插件會使用bean的自動裝配將業務類注入進來,其實業務邏輯控制器也不是Struts2建立的,而是Struts2的Spring插件建立的。默認狀況下,插件使用by name的方式裝配,能夠經過增長Struts2常量來修改匹配方式:設置方式爲:struts.objectFactory.spring.autoWire = typeName,可選的裝配參數以下:
a)          name:等價於Spring配置中的autowire=」byName」,這是缺省值。(框架會去Spring中尋找與Action屬性名字相同的bean)
b)         type:等價於Spring配置中的autowire=」byType」。
c)          auto:等價於Spring配置中的autowire=」autodetect」。
d)         constructor:等價於Spring配置中的autowire=」 constructor」。

就這個策略,我如今只知道利用byName來實現自動裝配。代碼以下:

<?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:p="http://www.springframework.org/schema/p"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"     default-autowire="byName" > <!-- beanEmp2中的具體類中有一個屬性empDao,存在geter和seter方法  -->     <bean id="Emp2" class="com.neusoft.leehom.action.EmpAction" > <!--        <property name="empDao" ref="EmpDaoImpl"></property>    -->     </bean>

    <bean name="empDao" class="com.neusoft.leehom.dao.EmpDao" ></bean>

</beans>

因爲上面empDao的名字相同,因此spring會直接根據注入屬性的名字查找與之同名字的bean。

 

       對於這兩種方式,Struts2都提供了對應的整合實現.比較這兩種整合方式,其本質是同樣的。不一樣之處在於,使用第二種自動裝配的方式時,因爲沒有在Spring中配置業務邏輯控制器,因此須要對其配置一些AOP之類的內容時就很難實現了。

 

http://blog.csdn.net/yangchaofeng1229/article/details/7918864

相關文章
相關標籤/搜索