Announcement Announcement Module
Collapse
No announcement yet.
Struts, Spring and Thread Safety Issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Struts, Spring and Thread Safety Issue

    Hi,

    Iam using struts + spring + hibernate


    I have an Action class , to which I pass the command object like this

    This entry goes into my action-servlet.xml file
    <bean name="/ReadOnlyAction" class="org.med.core.Command.ReadOnlyAction" singleton="false">
    <property name="commandObject"><ref bean="productDAO"/></property>
    </bean>


    This entry goes into my struts-config.xml file
    <action path="/ReadOnlyAction" type="org.springframework.web.struts.DelegatingAct ionProxy">
    <forward name="menu" path="/core/order.jsp"></forward>
    </action>


    Now If you notice the action class, the DAO is set as instance variable of my
    Action class , but struts strongly discourages this kind of design,

    In my application the same action class might get called with diffrent DAO impl,
    does this pose any danger to the thread safety etc, as Struts creates only one
    instance of action classes.

    Is this the best way to inject Spring managed beans into Struts Action classes ?

    public class ReadOnlyAction extends Action {

    /**
    * @return Returns the commandObject.
    */
    public Command getCommandObject() {
    return commandObject;
    }
    /**
    * @param commandObject The commandObject to set.
    */
    public void setCommandObject(Command commandObject) {
    this.commandObject = commandObject;
    }
    Command commandObject = null;
    ...
    ...
    ..
    .

  • #2
    Re: Struts, Spring and Thread Safety Issue

    Originally posted by hariharan1005
    Now If you notice the action class, the DAO is set as instance variable of my
    Action class , but struts strongly discourages this kind of design,
    Spring is handling the creation of your actions and since you have singleton="false" it will create a new Action for each request. Therefore, you don't need to worry about instance variables. If you were developing a "tranditional" Struts application - where Actions are single-threaded - you would have to worry about this.

    Comment


    • #3
      Properties versus model

      One thing to clarify here is the difference between setting controller bean properties (via dependency injection) versus passing objects in the model to a view. It is my understanding that the bean properties on a controller should be treated much like final variables; they provide useful information to the controller or provide a handle to a service, but should not be mutable. For data that changes on a per-request basis, objects should be passed in the model part of the ModelAndView (or, in the case of Struts, passed directly as request attributes). Using this strategy, you can make the bean a singleton and save the overhead of creating the bean on each request.

      Hope that helps,
      Derek

      Comment

      Working...
      X