The Source for Java Technology Collaboration
User: Password:



Meeraj Kunnumpurath

Meeraj Kunnumpurath's Blog

Resource injection in web applications

Posted by meeraj on December 15, 2005 at 02:35 AM | Comments (3)

I have been looking at the Servlet 2.5 specification (Maintenance Review). One of the key additions is the ability to inject dependencies to classes whose lifecycle are maintained by the container.

The types of resources that can be injected in are generally the ones currently available for lookup from the java:comp/env namespace, by defining them in the deployment descriptor, as specified by the Servlet 2.4 specification. Resource injection relies on J2SE 5.0 annotaions. J2SE 5.0 is a pre-requirement for Servlet 2.5.

Injectable Classes

The classes that can be injected with external dependencies are,

  • Servlets

  • Filters

  • Various lifecycle and event listeners
  • Resource Types

    The annotaions available for dependency injection are

  • @Resource: This annotation is used to inject resource types that were broadly covered by resource-ref, resource-env-ref and message-destination-ref elements in the deployment descriptor. This will include datasources, JMS administered objects etc.

  • @Resources: This annotation acts as a container for multiple @Resource annotations.

  • @EJB: This annotaion provides the same functionality as ejb-ref and ejb-local-ref elements for injecting EJB references

  • @WebServiceRef: This annotation is used for injecting web service references.

  • @InjectionComplete: This is a method level annotation for the method to be called once all the dependencies are injected. For the ones familiar with Spring, this is similar to the afterPopertiesSet method on the InitializingBean interface.
  • Example

    The snippet below shows a simple example of injecting a datasource into a Servlet,


    public class MyServlet extends HttpServlet {
    @Resource(name="myDs") DataSource myDataSource;
    }

    This injects a datasource by the JNDI name myDs into the field myDataSource. If the name attribute is not specified, the field name is treated as the JNDI name.

    Improvements?

    Would you want the resource injection mechanism to be extended to ay classes load by the web application classloader, rather than only those classes whose lifecycle are managed by the container. Would the Servlet specification require EJB 3 persistence contexts to be injectable to web application classes?


    Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
    Comments
    Comments are listed in date ascending order (oldest first) | Post Comment

    • Great to see that the Java Servlet Specification 2.5 will contain the notion of resource injection! I think this will make it more flexible and will make it easier to use them in a web app.. I think that it will be a good enhancement resource injection mechanism will be extended to any classes load by the web application classloader However, I think you do not want to use an EJB persistence context in a Servlet. I think you can better inject a EJB SessionFacade in your servlet, which has methods to access entity beans amd deal with persistency.. Separation of Concerns..I think..

      Posted by: tomhofte on December 17, 2005 at 08:25 AM

    • I think that in reality adding features like this are of very small incremental value. I can't remeber the last time I saw a project that wrote more than a couple of Servlets to handle special cases. It seems like almost everyone uses a framework that uses a dispatcher servlet/front controller of some description that routes requests to the appropriate Action or Component.

      The only objects that I tend to write these days that are implementations of Servlet API classes are a couple of Servlets to do things like download files where the framework makes it awkward, or a couple of Filters to to implement security, open-session-in-view etc.

      To make this useful (and even then I'd say it's questionable), I'd like to see an interface, say ResourceInjector, that frameworks like Spring, Pico etc. can implement to make Servlets and Filters first class objects in those frameworks. That way we could say goodbye to the proxy-servlet/filter pattern used to make that work today.

      Tim Fennell
      Stripes: Making web development as easy as it should be.

      Posted by: tfenne on December 18, 2005 at 05:06 PM

    • Everybody seems to praise resource injection and annotations those days, but I can't see the added value of having an @Resource(name="myDs") annotation over a ctx.lookup("myDs") API call. It looks just the joy of having a new cool syntax and use it everywhere, just like people to day wants to use XML at every place you could do with a simpler solution like .properties files.

      So please could someone point me to usefull use cases of those? Adding Syntax suggar to Java EE is very good for tool vendors and book writters. It's also very nice for checklist that compare Java EE x .NET without evaluatig the meaning and usefullness of each feature, but what about the real developer?

      Posted by: flozano on December 20, 2005 at 05:28 AM





    Powered by
    Movable Type 3.01D
     Feed java.net RSS Feeds