DWR, and Spring with multiple context files..

A couple of years ago, I was coding AJAX web apps the plain old way of creating an XMLHttpRequest object to communicate with the server and painfully parsing the XML responses… well it was fun for a thing or two, but once people started demanding more and more of the dynamic behaviour, it really got worse to code AJAX scripting from scratch… especially since I specialize in coding the server side components, I really didn’t want to be a full time javascript/HTML developer. That’s when I started to look around for some help in a form of open source scripting libraries and found DWR.

Note: this article does not speak about the details of implementing DWR, but rather talks about a specific situation. If you are interested in DWR, but are not aware of it already, I suggest you begin with the getting started guide from the creators themselves, available here..
http://directwebremoting.org/dwr/getstarted

As a full time java developer, DWR seemed to be a nice solution to my problem. I was really surprised by the ease of use and the time it took to get things started… did I mention the capability of integrating DWR directly with Spring beans 🙂 , there was no special configuration involved in working with spring beans.. until you come across a situation like I did, having multiple Spring Context files, which is quite normal once you start using more and more spring in your application and the ApplicationContext.xml really started to grow to an extent where it becomes to large and complex to deal with. Well, whatever the reasons, if you have multiple spring context files that your application is setup to work with, then the basic getting started guide will not be enough to use the spring beans directly with DWR. Here I’d tell you what’s that extra step that’s needed in such situations…

Well, that’s simple too… you’ll just have to add a couple of extra entries to the web.xml (if you don’t already have them).. a context-param named “contextConfigLocation” which will contain the links to all your spring context files, and a spring ContextLoaderListener. That’s it really.

So in addition to the usual DWR servlet and it’s servlet mapping, you will need the above two entries to make your DWR look at multiple spring context files.. Hope this helps someone.

pal 🙂

UPDATE:
As noted by commenter “Niksa Jakovljevic“, a much simpler way around this is to specify the location of the context file in the bean declaration of dwr.xml like this… (thanks Niksa Jakovljevic )

Also, if you are loading the spring contexts in other location (like struts-config.xml), then using the ContextLoaderListner in web.xml will load duplicate instances of the Spring beans which might lead to various problems….

  • Niksa Jakovljevic

    Nice. I would just like to add that the same can be done through dwr.xml by specifying parameter location, like this :

    Also I notice bug with above solution when you specify let say two spring beans from the same applicationContext xml file, DWR creates two instances of ApplicationContext which can cause big problems !!

    Regards,

    Niksa Jakovljevic

  • Niksa Jakovljevic

    Ups, i just notices that my code segments were escaped. Lets try again with [code]

    [/code]

  • Niksa Jakovljevic

    Ups again. Now submitting without angle brackets 🙂

    param name=”location” value=”/../applicationContext-beans.xml”. This comes under dwr create element.

  • coolpal

    oh.. thanks.. didn’t know that.

    pal 🙂