`

dwr spring 如何配置

阅读更多

DWR和Spring

让DWR和Spring一起工作的步骤

  1. 确认你用的是最新版的DWR。Spring创造器已经有了变化,所以你最好检查一下DWR的最新版本
  2. 确认你查看过开始指南中的内容。
  3. 确认你的Spring的Bean在DWR外面运行良好。
  4. 配置DWR和Spring一起工作。 (看下面)
  5. 查看演示页面: http://localhost:8080/[ YOUR-WEBAPP ]/dwr ,检查spring的Bean是否出现。

DWR对于Spring没有运行期依赖,所以如果你不使用Spring那么Spring的支持不会产生任何影响到。

The SpringCreator

这个创造器会查找spring的中配置的Bean,用Spring去创建它们。如果你已经在使用Spring,那么这个创造器会非常有用。

你可以通过下面的方式来创建远程调用的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*参数

如果你要在dwr.xml中指定使用哪些bean,你可以使用location*参数。你可以指定任意多个文件,只要参数以location开始并且唯一即可。例如:location-1, location-2。这些location被用做Spring的ClassPathXmlApplicationContext的参数:

<allow> 
... 
<create creator="spring" javascript="Fred"> 
<param name="beanName" value="Shiela"/> 
<param name="location" value="beans.xml"/> 
</create> 
</allow>

直接设置BeanFactory

SpringCreator有一个静态方法 setOverrideBeanFactory(BeanFactory) 用来通过编程的方式直接设置BeanFactory。

配置DWR和Spring

Bram Smeets写了一个有意思的blog,教你配置DWR使用beans.xml代替WEB-INF/web.xml。

我也对于如何在beans.xml中指定dwr.xml很感兴趣,尽管这看上去有些Spring传染病的感觉。有人知道如何实现吗?请加入邮件列表并告诉大家。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics