讓DWR和Spring一起工作
讓DWR和Spring一起工作的步驟
1. 確認你用的是***版的DWR。Spring創(chuàng)造器已經(jīng)有了變化,所以你***檢查一下DWR的***版本。
2. 確認你查看過開始指南中的內(nèi)容。
3. 確認你的Spring的Bean在DWR外面運行良好。
4. 配置DWR和Spring一起工作。 (看下面)
5. 查看演示頁面: http://localhost:8080/[ YOUR-WEBAPP ]/dwr ,檢查spring的Bean是否出現(xiàn)。
DWR對于Spring沒有運行期依賴,所以如果你不使用Spring那么Spring的支持不會產(chǎn)生任何影響到。The SpringCreator這個創(chuàng)造器會查找spring的中配置的Bean,用Spring去創(chuàng)建它們。如果你已經(jīng)在使用Spring,那么這個創(chuàng)造器會非常有用。
你可以通過下面的方式來創(chuàng)建遠程調(diào)用的Bean:
- <allow>
- ...
- <create creator="spring" javascript="Fred">
- <param name="beanName" value="Shiela"/>
- </create>
- </allow>
尋找你的Spring配置
有三種方式尋找配置文件:
ContextLoaderListener
最簡單的方式是使用org.springframework.web.context.ContextLoaderListener。你不必使用所有的Spring-MVC功能,只需要這個Listener就夠了,所以這是個不錯的方案。你需要在WEB-INF/web.xml中做如下配置:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class> org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
我能找到的ContextLoaderListener的***的文檔就是javadoc。如果你知道還有更好的文檔,請告知我。
Rob Sanheim 指出還有一個能深入了解ContextLoaderListener的文檔。
使用location*參數(shù)
如果你要在dwr.xml中指定使用哪些bean,你可以使用location*參數(shù)。你可以指定任意多個文件,只要參數(shù)以location開始并且唯一即可。例如:location-1, location-2。這些location被用做Spring的ClassPathXmlApplicationContext的參數(shù):
- <allow>
- ...
- <create creator="spring" javascript="Fred">
- <param name="beanName" value="Shiela"/>
- <param name="location" value="beans.xml"/>
- </create>
- </allow>
直接設置BeanFactory
SpringCreator有一個靜態(tài)方法 setOverrideBeanFactory(BeanFactory) 用來通過編程的方式直接設置BeanFactory。
配置DWR和Spring
Bram Smeets寫了一個有意思的blog,教你配置DWR使用beans.xml代替WEB-INF/web.xml。
【編輯推薦】