spring webwork 集成 三種方法
這種方法看起來比較煩瑣,(這里描述的和spring文檔里的有些區(qū)別,這種方法可按spring文檔里的做法)
- < listener > ? ??
- ???????? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ? ??
- </ listener > ? ??
- ??
- < listener > ? ??
- ???????? < listener-class > com.opensymphony.xwork.spring.SpringExternalReferenceResolverSetupListener </ listener-class > ? ??
- </ listener > ??
第二步:在Spring里面配置類似Dao之類的bean,例如?
- < bean ? id = "myDAO" ? class = "com.ryandaigle.persistence.MyDAO" ? singleton = "true" ? /> ??
第三步:配置XWork.xml,例如
- < package ? name = "default" ? extends = "webwork-default" ? ??
- ???????? externalReferenceResolver = "com.opensymphony.xwork.spring.SpringExternalReferenceResolver" >
- ???????? < interceptors > ? ??
- ???????????????? < interceptor ? name = "reference-resolver" ? class = "com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor" /> ? ??
- ???????????????? < interceptor-stack ? name = "myDefaultWebStack" > ? ??
- ???????????????????????? < interceptor-ref ? name = "defaultStack" /> ? ??
- ???????????????????????? < interceptor-ref ? name = "reference-resolver" /> ? ??
- ???????????????? </ interceptor-stack > ? ??
- ???????? </ interceptors > ? ??
- ???????? ??
- ???????? < default-interceptor-ref ? name = "myDefaultWebStack" /> ? ??
- ???????? ??
- ???????? < action ? name = "myAction" ? class = "com.ryandaigle.web.actions.MyAction" > ? ??
- ???????????????? < external-ref ? name = "DAO" > myDAO </ external-ref > ? ??
- ???????????????? < result ? name = "success" ? type = "dispatcher" > ? ??
- ???????????????????????? < param ? name = "location" > /success.jsp </ param > ? ??
- ???????????????? </ result > ? ??
- ???????? </ action > ????????????????? ??
- </ package > ???
2.SpringObjectFactory
我一直用這種方法,因為以前覺得是xwork本身提供的方法,升級有保障.
配置方法:
第一步.在spring的 applicationContext.xml (根據(jù)實際情況決定) 里面定義你的action,例如?
- < bean ? name = "some-action" ? class = "fully.qualified.class.name" ? singleton = "false" > ? ??
- ???? < property ? name = "someProperty" > < ref ? bean = "someOtherBean" /> </ property > ? ??
- </ bean > ???
可以看到,可以使用Spring的特性來給你的action設置屬性等,當然也可以使用Spring的攔截器等 (可以使用不一定等于推薦使用)
注意一定是singleton="false",因為xwork的action是這樣要求的.
第二步.在xwork.xml里定義你的action定義?
- < action ? name = "myAction" ? class = "some-action" > ? ??
- ???? < result ? name = "success" > view.jsp </ result > ? ??
- </ action > ???
?第三步.要使上面的關聯(lián)生效,還要用我們的SpringObjectFactory來替換Xwork的默認ObjectFactory.
最新的SpringObjectFactory里面,有兩種方法,其中我覺得A方法更直觀一些.
A:修改web.xml
- <!--?這個是spring的listener,可以改為你的自定義的spring的Listenter?--> ? ??
- < listener > ? ??
- ???????? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ? ??
- </ listener > ? ??
- ??
- ??
- <!--?這個必須在?Spring?ContextLoaderListener?的后面?--> ? ??
- < listener > ? ??
- ?? < listener-class > com.opensymphony.xwork.spring.SpringObjectFactoryListener </ listener-class > ? ??
- </ listener > ??
?B.在spring的applicationContext.xml (根據(jù)實際情況決定)里面定義一個bean,例如?
- < bean ? id = "spring-object-factory" ? class = "com.opensymphony.xwork.spring.SpringObjectFactory" ? ??
- ???????? init-method = "initObjectFactory" /> ??
這樣Spring會自動調用initObjectFactory方法來替換Xwork的默認ObjectFactory
3.ActionAutowiringInterceptor
這個方法是最近出現(xiàn)的,可能是最簡潔的方法,但是不知道性能上有沒有問題,我覺得jdk1.4以后應該沒有任何問題吧,至于實際效果你的自己測試一下.
第一步:配置web.xml?
- <!--?這個是spring的listener,可以改為你的自定義的spring的Listenter?--> ? ??
- < listener > ? ???
- ???? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ? ??
- </ listener > ? ??
???????
?第二步配置xwork.xml里面的攔截器
- < interceptors > ? ??
- ?? < interceptor ? name = "autowire" ? class = "com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor" > ? ??
- ???? < param ? name = "autowireStrategy" > @org.springframework.beans.factory.config.AutowireCapableBeanFactory@AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE </ param > ? ??
- ?? </ interceptor > ? ??
- ?? < interceptor-stack ? name = "autowireDefault" > ? ??
- ???? < interceptor-ref interceptor-ref = "autowire" ? /> ? ??
- ???? < interceptor-ref interceptor-ref = "defaultStack" ? /> ? ??
- ?? </ interceptor-stack > ? ??
- </ interceptors > ???
????????????????你的攔截器里都要包含autowire,例如這個autowireDefault就相當于以前的默認的攔截器了.
其中的攔截器策略可以配置,上面配置的是根據(jù)類型,如果不配置,默認是根據(jù)名字.
一共有四種策略:
AUTOWIRE_CONSTRUCTOR
AUTOWIRE_BY_TYPE
AUTOWIRE_BY_NAME
AUTOWIRE_AUTODETECT
這種方法執(zhí)行原理就是查找你的action的所有字段,如果和Spring的定義bean有相同的,就自動設置.
假設你的Spring的applicationContext.xml里面有這樣一個定義:
- < bean ? id = "userManager" ? class = "com.test.UserManager" ? /> ???
?如果你在xwork.xml 里面定義的某個action有一個字段叫userManager,那么在運行時刻就會自動被設置為Spring的配置文件里定義的Bean.
posted on 2010-04-12 14:23 飛熊 閱讀(250) 評論(0) 編輯 收藏 所屬分類: struts2.0