Sep

14

A Refinement to Integrating Spring and Struts

Posted by Keith McMillan

September 14, 2009 | Leave a Comment

Some time ago, I posted an article on how to enable dependency injection with a Spring and Struts application.  In the meantime, I’ve come across an inefficiency in that article, and wanted to update my loyal readers (both of you 😉

In the previous article, I mentioned that you create Spring beans for each of your actions in the Struts-Config.xml file. Two such beans could look like this:

<bean name=”/login” class=”com.adeptechllc.myapp.view.action.LoginAction”/>

<bean name=”/logout” class=”com.adeptechllc.myapp.view.action.LoginAction”/>

and beans such as this would work fine, except for a subtle point: because we’ve listed two different beans, but the same class, we’d actually get two instances of the LoginAction created.  Since typically we want only a single instance of our actions, this could cause unexpected and difficult to diagnose problems later.

In order to only create a single instance of the LoginAction class, you can use an alias in Spring.  I’ve started creating a single bean for each action, the aliasing the Struts actions to that single instance,  like this:

<bean id=”LoginAction” class=”com.adeptechllc.myapp.view.action.LoginAction”/>

<alias alias=”/login” name=”LoginAction”/>

<alias alias=”/logout” name=”LoginAction”/>

You can also simply provide more than one name or ID directly in the first bean definition, but if you have a lot of URLs directed at a single action handler, I find this easier to manage.


Comments

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.

Blogroll