Configuring DWR 3.0 with spring using annotations
I’m a fan of the spring framework and I have recently become a fan of DWR as well. Both of them together are just unstoppable, they take ajax and bean exposure through javascript to a new level. I have written about my experiences integrating the two and about validating forms using ajax. I’d advise you to read at least the integration article before continuing.
Using annotations to configure the spring container is a very useful feature, a majority of developers prefer to use annotations rather than XML files. However, annotation driven configuration of DWR through spring was not possible in older versions of DWR. The new version however, allows such configuration and with ease I might add. You can now use DWR with spring with almost no configuration in your XML files(a little bit of configuration is required but it doesn’t go beyond 10 lines or so). The best part however is that you don’t need to configure a seperate servlet for DWR at all, all calls to dwr can be routed through the spring dispatcher servlet which helps cut down configuration even more.
First the bean which we want to expose :
@Service
@RemoteProxy
public class MyBean {
@RemoteMethod
public List<Integer> getStuff() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 100; i++) {
list.add(i);
}
return list;
}
}
The @Service annotation tells spring to initialize this bean as a service. The @RemoteProxy annotation is what tells DWR that we want to expose this bean to the client. Notice the @RemoteMethod annotation, thats what tells DWR to expose this particular method. Only those methods which have this annotation will be exposed by DWR, nothing else. You are free to use as many annotations as you’d like, you can make the bean transactional as well with the @Transactional annotation and still have the bean exposed.
Next comes the web.xml configuration :
<servlet> <servlet-name>dwrexample</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet-mapping> <servlet-name>dwrexample</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>dwrexample</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Notice how we do not initialize the org.directwebremoting.spring.DwrSpringServlet or the org.directwebremoting.servlet.DwrServlet. Previous configurations, which required you to list the classes to scan for annotations, are no longer required due to the namespace support provided in the latest versions. Also note how we map all requests to /dwr/* to the spring dispatcher servlet.
Next comes the configuration required by the DispatcherServlet, dwrexample-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> <context:annotation-config /> <tx:annotation-driven /> <dwr:configuration /> <dwr:annotation-config /> <dwr:url-mapping /> <dwr:controller id="dwrController" debug="true" /> <context:component-scan base-package="com.codercorp.dwr,com.codercorp.displaytag" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="order" value="1" /> </bean> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="order" value="2" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
I’m not going to explain the spring related configurationm but just the DWR related config.
<dwr:annotation-config />
asks dwr to scan spring managed classes for annotation related to its configuration, @RemoteProxy and @RemoteMethod. Remember that the element will NOT scan any classes not managed by spring. The element
<dwr:url-mapping />
asks DWR to map the util.js and engine.js files to the dwrController. The element
<dwr:controller id="dwrController" debug="true" />
defines the dwrController. The line
<dwr:configuration />
is not required in this particular instance since we aren’t going to configure any beans which aren’t managed by spring but if you need to do so, you must specify such configuration under this element.
Do note the order properties which have been added to the spring config beans, these properties are required for proper functioning of the example. One last thing, the dwr test page which lists out your exposed beans etc no longer works. However, the test pages for the actual beans do work. So if the page http://YOUR-SERVER/YOUR-APP/dwr/ shows blank, don’t panic, just browse over to, http://YOUR-SERVER/YOUR-APP/dwr/test/YOUR-BEAN-NAME and you’ll see the list of exposed methods.
Thats it, you’re good to go. I was stumped by the test page not displaying for a little while so it took me a day to get all this working. Feel free to leave a comment if this has made your life any easier or if you have any questions.
No related posts.

Thanks! I just switched to spring’s annotations and wanted to use DWR in a similar fashion. So that’s exactly what I was looking for.
Notice that if you couple @Service and @RemoteProxy a good alternative would be to use and remove some lines altogether.
Fan-fairy-tatstic! Just what I was after…
Now to get a convter going for a custom value object…
@RemoteMethod cannot be resolved to be a type is coming when i am trying to use
@RemoteMethod
Do you know how I can disable the DWR test page? Why would I want users to be able to see that page? I want it gone!
Just do debug=”false”
ha..thanks!
Hi,
I have tried the same, but I got the following message at the start-up:
Error creating bean with name ‘__dwrConfiguration’: Cannot resolve reference to bean ‘__’ while setting bean property ‘creators’ with key []; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘__’: Cannot create inner bean ‘(inner bean)’ of type [org.directwebremoting.spring.BeanCreator] while setting bean property ‘creator’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘(inner bean)’: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
Do you have any idea?
Thanks in advance!
I have the same problem.
I’ve resolved this by supplying name to @RemoteProxy like this:
@Service
@RemoteProxy(name=”test-service”)
public class TestService { … }
You can check code in DwrAnnotationPostProcessor:
String javascript = annotation.name();
…
String creatorConfigName = “__” + javascript;
beanCreator.addPropertyValue(“javascript”, javascript);
Seems like no default value is provided if not specified explicitly.
Hello
I’m getting: Javascript name MyBeanRemote is used by 2 classes (lt.cu.ebank.office.web.controllers.MyBean and BeanCreator[MyBeanRemote])
The bean is as follows:
@Service(“MyBeanController”)
@RemoteProxy(name=”MyBeanRemote”)
public class MyBean
If i remove @Service(“MyBeanController”) and going to url :/dwr/test/MyBeanRemote i’m getting spring exception: No adapter for handler : Does your handler implement a supported interface like Controller?
If i change @Service to @Controller i’m getting : Javascript name MyBeanRemote is used by 2 classes.
This is exactly what I’d like to do (use a @Controller bean instead of a @Service bean). The reason is that I’d like to make an AJAX request that changes an HTTP Session Attribute.
Currently I get the ‘No bean named ‘XXXX’ is defined’, which I had gotten before with my @Service, but fixed by setting lazy-init=true on my dwrController. This isn’t fixing it when referencing a @Controller bean.
Any ideas on the best way to do this?
Scratch that…I still had the 2.x jar, with 3 it’s working. (now to figure out how I can call the @Controller’s method with a parameter annotated as:
@RequestMapping
@RemoteMethod
public Object controllerMethod (@ModelAttribute(“attribX”) Object attribX)
and have the attribute injected properly by spring when DWR calls it…(is this even possible?)
Ofcourse it’s possible, infact it’s the entire point of the integration.
Well, has been a while since this post, but there wasn’t really any other source for this. The problem is (I think) that the bean is defined in two different contexts. I moved everything from the global applicationContext.xml to [..]-servlet.xml, and now it works.
Sorry for the late reply.
The error generally appears when you’re mixing DWR+Spring configuration. Also, make sure that the order in which you define your beans is the same as described in the example.
Thanks, After searching around the net for ages, I finally came upone this.
Theres nothing to help people with DWR 3 and Spring 3 integration.
So you set me on the right track.
At first I was confused what was going on campared to DWR 3 + Spring 2.x integration.
But its all good now.
Works for @Controllers
and as you said watch the order of the configuration tags.
Thanks
Thanks for your comment, happy to be of help.
I think that this line is not working, base-package parameter accept only one package, you can set more context:component for more packages.., for this example it will be work, but package com.codercorp.displaytag will not be included in scan, only first one! maybe it helps someone!
Hi,
I was trying out the integration of DWR 3.0 and Spring 2.5.5. I have some doubts.
1. Will the annotations alone enough without use of tags for service beans? If so, what all configurations are to be done.
2. dwr:component-scan is not at all working for me.
can you please help me?
Regards,
Shiby
Let me go ahead and give a warning for everyone, because I fought this for 10 hours, and wanted to shoot myself and DWR.
In Spring MVC, if you write your own WebBindingInitializer (which is the same thing as @InitBinder, and is used by ALL your controllers) and intialize it via:
(google it if you are lost)
This will NOT work with DWR. the DwrController ends up with ONLY this handler, and none of the other handlers exist in Spring, causing nothing to work. It blows whether annotated, or the 2.x way.
just integrate the DWR to SpringMVC-SWF but it turns to extremely slow page loads..
any hints guys?
i also changed the debug value.
but still extremely slow performance.
Hi! Thanks for the post…
What version of DWR are you using? I’m trying to use 3.0.0.RC1 and 3.0.M1, but i get errors loading the XML definition:
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘dwr:configuration’.
Any idea?
Thanks!
Cronida
Well… My problem was I could not find the xsd. Now I get it locally.
I am having the following error:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionParser for element [annotation-config]
Again… Any idea?
Cronida
Make sure you have the right version of DWR jar on server class path.
org.directwebremoting
dwr
3.0.M1
I try to do uptade Dwr 2.0 to 3.0 and i have this error :
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spoople-servlet.xml]; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.directwebremoting.spring.DwrNamespaceHandler]: Is it an abstract class?; nested exception is java.lang.InstantiationException
can yo help me?
Make sure you only have one version of DWR jar. Which should be
org.directwebremoting
dwr
3.0.M1
This tutorial is great!
I popped into one error:
javax.servlet.ServletException: No adapter for handler [org.directwebremoting.spring.DwrController@….
But that was simply fixed by adding:
into the spring beans xml file
What did u add to fix this issue?
Vishal, your post got cut off!!!! what did you do to fix this error?
Hi,
I feel this integration won’t work,
if someone knows how to integrate using annotations mentioned above please provide a sample working project
thanks
Can you please give an example on the JSP how do we invoke DWR method exposed using @RemoteProxy. I know how to do it in DWR and spring 2.5 but not for DWR 3.0 and Spring 3.0 together when used. The article you have written is too good, but I don’t know abt the JSP how I should invoke
I’ve just migrating from DWR 2 into DWR 3.0.
I follow as you describe and viola when I kick url <http://localhost://test/ItemController
I’ve got message on page ” Error: java.lang.SecurityException: No class by name: ItemController”.
Any ideas? since I already map using @Controller in my bean.
Rgds,
Aries
I add configuration on applicationContext.xml as describe below :
dwrController
Than i kick url : http://localhost:8084/SpringMVCItemCategoryWiringAnnotationDWR/dwr
Viola now I have list about classes known to DWR. But none class controller on there even I already mark with @RemoteProxy and @RemoteMethod.
Error message as describe on my previous comments.
Any ideas?
Rgds,
Aries
Hi,
can anybody tell me how can I integrate ExtJs with DWR and Spring.
I already done it with DWR 2 and Spring 2.5. But I am looking it with DWR 3 and Spring MVC. If you guys have any idea please share it.
Hi,
I integrate DWR 3.0 with Spring 2.5 and its working fine for me. Now I am looking for ExtJs with DWR and Spring. Can anybody tell me how can I do that. I already done a mock example but I am not pretty sure about it. If any one have any idea please share it.
I want to use Spring 3 and dwr 3 and their annotated capabilities. I set all spring properties (web.xml , servlet) , annotate controllers.
When I want to use DWR with annotations integrated in spring i get :
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Duplicate name found. See logs for details.
at org.directwebremoting.spring.SpringConfigurator.configure(SpringConfigurator.java:143)
…………….
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘DwrAnnotationURLMapper’: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘__dwrController’: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Duplicate name found. See logs for details.
I can’t find solution.
Excellent guide to get me up and running,
1 comment though was that the section “Next comes the configuration required by the DispatcherServlet, :” states to update dwrexample-servlet.xml, I found that I needed to put these updates in the /WEB-INF/applicationContext.xml file referenced from the web.xml.
I’m trying to make this work with Spring 3.0.3′s mvc:annotation-driven approach, but have failed so far with exception:
javax.servlet.ServletException: No adapter for handler [org.directwebremoting.spring.DwrController@36d2b3]: Does your handler implement a supported interface like Controller?
The problem is that with mvc:annotation-driven directive, only classes annotated with @Controller stereotype can act as controllers in Spring, but it seems that DwrController is not annotated with @Controller.
Any workaround for this? (aside from modifying the dwr framework to add @Controller to class DwrController)
Thanks,
Enrico
If any one is using Spring ROO, use the below URL to access your test dwr page
( missing /dwr )
http://YOUR-SERVER/YOUR-APP/test/YOUR-BEAN-NAME