Spring 和Struts 2都是應用非常廣泛的J2EE應用程序框架,Struts 2主要注重的是將視圖層和控制層分開,
但是不涉及對模型層的優化設計;而Spring除了實現Struts 2的MVC功能外,還可以利用其控制反轉的思想實現對模型層的優化,從更深層次去降低應用程序各個組件的耦合程度。
1、添加ContextLoaderListener到web.xml
-
<
listener
>
-
<
listener-class
>
-
org.springframework.web.context.ContextLoaderListener
-
</
listener-class
>
-
</
listener
>
-
如果需要添加多個spring配置文件就要在web.xml加入contextConfigLocation這個context參數
-
<
context-param
>
-
<
param-name
>
contextConfigLocation
</
param-name
>
-
<
param-value
>
-
/WEB-INF/classes/applicationContext-*.xml
-
</
param-value
>
-
</
context-param
>
-
<
context-param
>
-
<
param-name
>
contextConfigLocation
</
param-name
>
-
<
param-value
>
-
classpath:beans.xml
-
</
param-value
>
-
</
context-param
>
-
ContextLoaderListener的作用 :
ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置信息。因為它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啟動容器時,就會默認執行它實現的方法。
在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。
(
org.springframework.web.context.ContextLoaderListener創建出 WebApplicationContext容器對象, 并將創建出來的WebApplicationContext對象存儲進了Web應用程序的application作用域中,存儲時的key為 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE這個常量表示的字符串,以后在Web應用程序中就可以使用
application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) ;或
Spring提供的工具方法 WebApplicationContextUtils.getWebApplicationContext(application)來獲得 spring容器對象
<context-param>(
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
)
<context-param>
節點就是用來為這個全局對象配置參數的,
這樣就可以在整個項目中都能使用,spring就是通過servletConfig來讀取web.xml中的路徑參數來過得beans.xml等一些spring的xml文件的。
用來定位Spring XML
文件的上下文配置。
ContextLoaderListener使用contextConfigLocation這個ServletContext初始化參數來指定WebApplicationContext容器對象的配置文件,如果沒有配置
contextConfigLocation
這個 ServletContext的初始化參數,ContextLoaderListener則默認使用/WEB-INF /applicationContext.xml作為配置文件。
以上是web.xml,下面是修改struts.xml
Struts 2在發布的時候在其插件包struts-2.1.2\lib中有struts2-spring-plugin-2.1.2.jar,正是它實現了Struts 2和Spring的整合。這個插件覆蓋了Struts 2的ObjectFactory,所以在Struts 2創建一個對象的時候,例如Action類,它會先到Struts 2的配置文件去尋找類的名字,然后轉到Spring配置文件中去查找名字找到該類。
當創建一個對象的時候,它會用Struts2 配置文件中的class屬性去和Spring配置文件中的id屬性進行關聯,如果能找到,則由Spring創建,
否則由Struts 2框架自身創建
,
然后由Spring來裝配
。Spring插件具體有如下幾個作用:
— 允許Spring創建Action、Interceptror和Result。
— 由Struts創建的對象能夠被Spring裝配。
— 如果沒有使用Spring ObjectFactory,提供了2個攔截器來自動裝配action
。
在這個插件包中有個
struts-plugin.xml
文件,它的內容如下:
<struts>
<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"/>
</interceptors>
</package>
</struts>
其中設置了Struts 2框架常量struts.objectFactory的值為spring,默認情況下action是有struts2的工廠生產的.在這里把它交給了Spring生產
這里它將框架常量
struts.objectFactory
覆蓋了,設置為
”spring”
,其實這里是使用了縮寫,我們可以寫全稱:
org.apache.struts2.spring.StrutsSpringObjectFactory
。這個縮寫的
”spring”
是和
bean
配置中的
name
屬性相對應的。
默認情況下所有由框架創建的對象都是由
ObjectFactory
實例化的
,
ObjectFactory
提供了與其它
IoC
容器如
Spring
、
Pico
等集成的方法。覆蓋這個
ObjectFactory
的類必須繼承
ObjectFactory
類或者它的任何子類,并且要帶有一個不帶參數的構造方法。在這里我們用
org.apache.struts2.spring.StrutsSpringObjectFactory
代替了默認的
ObjectFactory
。
如果
action
不是使用
Spring ObjectFactory
創建的話,插件提供了兩個攔截器來自動裝配
action
,默認情況下框架使用的自動裝配策略是
name
,也就是說框架會去
Spring
中尋找與
action
屬性名字相同的
bean
,可選的裝配策略還有:
type
、
auto
、
constructor
,我們可以通過常量
struts.objectFactory.spring.autoWire
來進行設置。
Struts 2框架整合Spring后,處理用戶請求的Action并不是Struts框架創建的,而是由Spring插件創建的。創建實例時,不是利用配置Action時指定的class屬性值,根據bean的配置id屬性,從Spring容器中獲得相應的實例。
注意:
strut.xml
中配置的
action
的
class
值,應該與
spring
中配置的
Action Bean id
相應。
<filter>
<filter-name>f</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring與Struts2整合原理