Spring集成Struts的方法
struts要和spring集成,struts就必須能訪問(wèn)spring的上下文,struts作為web的框架,故要保證web應(yīng)用程序啟動(dòng)前裝載了spring的web應(yīng)用上下文。如果裝載了spring的web上下文,在程序中就可以通過(guò)spring提供的WebApplicationContextUtils工具類(lèi)來(lái)訪問(wèn)該上下文:
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
裝載spring上下文的方法有以下幾種:
1、通過(guò)web.xml聲明監(jiān)聽(tīng)器,在web服務(wù)啟動(dòng)時(shí)裝載: (其listener用到context-param)
...................
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
.......................
2、通過(guò)web.xml聲明spring提供的ContextLoaderServlet(早期不支持servlet2.4時(shí)用):
...................
<servlet>
<servlet-name>contextLoader</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...................
3、通過(guò)struts-config.xml以插件的形式聲明spring提供的ContextLoaderPlugin,就可以向sturts的ActionServlet裝載spring的應(yīng)用上下文了:
......................
<plug-in className="org.springframework.web.struts.ContextLoaderPlugin">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>
.........................
集成struts的方法:
1 、使用 org.springframework.web.struts.ActionSupport 類(lèi):
這是最簡(jiǎn)單的集成struts方法。通過(guò)struts的action簡(jiǎn)單繼承ActionSupport類(lèi)便可以直接使用spring上下文。
..............
XXXX xxx=(XXXX)this.getWebApplicationContext().getBean("xxx");
..............
2、使用 org.springframework.web.struts.DelegatingRequestProcessor 類(lèi)來(lái)覆蓋struts的高層處理類(lèi)RequestProcessor,這就要在struts-config.xml中用controller聲明:
.....................
<action-mappings>
...........
<action path="/login"
type="struts.action.LoginAction"
name="loginForm">
<forward name="success" path="/WEB-INF/main.jsp">
</action>
...........
</action-mappings>
.....................
<controller processorClass="org.springframework.web.struts.DelegatinRequestProcessor"/>
.....................
另外,還要在spring配置文件中,聲明對(duì)應(yīng)某個(gè)action的bean,而且bean的name值必須和action的path一致,因?yàn)閟pring容器是根據(jù)此bean的name來(lái)找到相應(yīng)的action,然后再進(jìn)行相應(yīng)的注入操作的:
................
<bean id="loginService" class="business.LoginServiceImpl">
<property name="name" value="aaaaa"/>
</bean>
<bean name="/login"
class="struts.action.LoginAction">
<property name="loginService">
<ref bean="loginService"/>
</property>
</bean>
................
3、使用 org.springframework.web.struts.AutowiringRequestProcessor 類(lèi),它是spring2.0 后加入的。類(lèi)似DelegatingRequestProcessor,但它的自動(dòng)完成能力更強(qiáng)。首先也要在struts-config.xml中用<controller>聲明:
........................
<controller processorClass="org.springframework.web.struts.AutowiringRequestProcessor"/>
........................
其他的就照常就得了,它比DelegatingRequestProcessor少了在spring配置中聲明action的bean,它會(huì)根據(jù)action類(lèi)名來(lái)自動(dòng)完成注入操作。即Struts的Action中如有某業(yè)務(wù)類(lèi)A對(duì)象的屬性,又在spring配置文件中聲明了業(yè)務(wù)類(lèi)A的Bean,則AutowiringRequestProcessor就會(huì)自動(dòng)將業(yè)務(wù)類(lèi)A的Bean注入到Action中。
看上去你好像啥都沒(méi)做,而事實(shí)上,注入工作已經(jīng)由AutowiringRequestProcessor自動(dòng)完成。 這種autowire的注入支持兩種不同的方式,分別是byName和byType,默認(rèn)是byType。