Spring + Struts Should I inject?!


Web tier: servlets, JSP, Web frameworks: Spring + Struts Should I inject?!

  1. Spring + Struts Should I inject?! (2 messages)

    I would like to hear(read) from you guys what is industry best practice and the benefits of two different types of accessing bernate Manager beans.

    Case 1)
      I have an Action class and I have an actionServlet.xml file which contains mappings for the hibernate manager beans that I will be using in my Action class.
    As you will know my action class would also need to have getters and setters for those managers that I have mapped against it self in the actionServlet.xml
    However in MVC it is not recommended for the Action class to do business logic ie interacting with managers. specially when you have to interact with several managers and perform some form of business logic. As such i have a form of a delegate pattern being used.
    ie the Action receives the request and uses a delegate which will then use a parser to parse the request and return the value i want my action to wrap on a request and make it thus available to a JSP.
    However for this to happen I needed my delegates to have a reference to my Managers that can only be injected at the action class level.
    Thus I will have code like this.
      MyDelegate del = MyDelegate.getInstance(myManager,myOtherManager);

    Personally this for me becomes a bit cumbersome as I need to write mappings for managers in the actionServlet.xml and then write getters and setters on the action?!

    Ok case 2)
    Assuming the same scenario above but instead of injecting the managers (ie of using the actionServlet.xml) I have my Action class generating a spring WebApplicationContext object.
    Thus...i have this code:
    ServletContext servlet_context = super.getServlet().getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servlet_context);
    From what i read this 'wac' object contains all the references of my managers I thus pass this wac object down to my delegate and then my delegate can gain access to my managers.

    I prefer the 2nd scenario as is less code to manage ( ie no ActionServlet.xml mappings) and if u want to interact with 8 managers your delegate constructor doesnt need to be massive in parameters.

    I should stress the following my web.xml file has a classpath reference to the xml-mapping file that contains mappings of all the managers that exist and it is because of this that my WAC object has access to those managers.

    So I basically want to know what is the best strategy am is case 2 better than 1 and why? or why not?
    What benefits could i get in using actionServlet.xml??
    Thanks guys!
  2. IMHO your delegate should be a regular business object itself, so make use of dependency injection and define it and its dependencies in spring's application context. I haven't used Struts (prefer Tapestry ;)) but I think all you need is the reference to the ApplicationContext object in your web tier (i.e. Action classes). Then you just get a proper bean, call business method passing in arguments (from the request) and don't care about depencies.

  3. oh i use spring applicationContext but when u use struts u have to define another applicationContext file (ActionServlet.xml) which in conjunction with the struts spring plugin the action classes can gain access to the managers.
    However my basic question is...why should i use this second context file (ie ActionServlet.xml) when i can gain access to the managers via Spring's WebApplicationContext object?!

    10nks ;)