DWR Dependency Injection with Spring

Once you use Spring’s dependency injection you will probably never want to go back to building applications without it, and at the time of this writing the DWR documentation for doing this was not smooth at all. I spent a few hours trying to figure it out and it turns out to be a very straight forward thing to do.

The DWR-Spring integration is very easy.

First, On the dwr.xml file, add your ajax class using the “spring” creator, and add to it a param with name “beanName” (this is not an arbitrary name. You have to name it exactly like that, since that’s how DWR recognices which class will be initialized trough dependency injection). The param value, which in this case is “exampleAjax” is the name of the bean specified in spring’s configuration file (which we will see in a few moments)

<create creator=”spring” javascript=”ExampleAjax”>
<param name=”beanName” value=”exampleAjax”/>

That’s it for DWR configuration. Now let’s get to spring.

First of all, I want to make a parenthesis to say that, as a good practice, I advice you to create an independent spring configuration file for the DWR classes to avoid having a long and hard to mantain xml. So, back to the point of this post, in the spring beans configuration xml, add a bean as follows:

<bean name=”exampleAjax”
<property name=”exampleService”>
<ref bean=”exampleService”/>

The explanation of this snippet:

  • bean name: must match DWR’S param value
  • class: The class path to the class containing your ajax helper methods (the ones that you will call using DWR)
  • property name: A variable that resides inside the class. In this case, my class (ExampleAjax) has a variable of type ExampleService, which contains business logic, so I want Spring to initialize it trough dependency injection
  • ref bean: the spring’s bean name that will be asigned to the “property name” variable.

So that’s it. This integration is 100% transparent. You can now be sure that every time you call DWR methods in that AjaxEnabled-2.0-EndlessJavascriptNightmare page of yours will be memory efficient thanks to Spring’s Dependency Injection.

2 thoughts on “DWR Dependency Injection with Spring

  1. Hi,
    Really It is very good to understand the DWR Dependency Injection with Spring.
    Thanks for sharing.

    I followed same but i am getting the null object in Session factory. [If i call from Spring MVC It is working fine. If you call from DWR It is showing the NULL object.]
    Please help me to fix this problem.

    Anandan R

    • Now It is working fine.
      The issue is not with Session Factory. In hibernate query it was wrong. I corrected the query now It is working fine.

      Thank you so much for your help.

      Anandan R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s