DWR, and Spring with multiple context files..
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..
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.
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext.xml /WEB-INF/classes/transaction.xml /WEB-INF/classes/dao.xml /WEB-INF/classes/strutsBeans.xml /WEB-INF/classes/whatever.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
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.
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….