The Source for Java Technology Collaboration
User: Password:



Brian Repko

Brian Repko's Blog

EJB3 Dependency Injection vs Lookup

Posted by brianrepko on March 16, 2005 at 05:00 PM | Comments (3)

I put together an initial list of issues that I have with the use of injection for EJB dependencies one night because I couldn't sleep. In short, there are so many issues that I feel it should be removed and replaced with dependency lookup through the context object. Anyway, a co-worker of mine told me about 3 weeks ago that he was going to the TSS conference. So, I polished off the paper so that he could share with all the EJB3 and Spring folks out there. I don't what the reaction was, but I must say that it was good for me to clarify the issues. I wanted to put the paper on the blog and will also email to the JSR feedback email. I feel a bit like Martin Luther posting his 95 theses. You can find the paper here. Comments are very welcome.

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

  • Your argument against using dependency injection for everythign is well founded. I completely agree.

    Dependency injection works great for the context object. It's simple and it provides the EJB with full access to all the server resources as necessary. But it is completely unnecessary to inject more than one resource! A mountain of setter methods is not an elegant solution.

    Posted by: jessewilson on March 17, 2005 at 09:32 AM

  • If you live entirely in EJB world, then perhaps your concerns are very much valid. EJB and DI and for that matter anything other than EJBs do not mix well together!
    If you look into DI without EJB goggles, you can see tremendous benefit it can offer. I prefer constructor based DI over setter based DI. First benefit I have experienced, and this is the experience of others as well who have had serious implementation done, is that it changes the way one designs the application. It forces you to consider the right structure and dependencies and think patterns. Side effect is the inbuilt extensibility and flexibility it brings to the table. One ends up writing very concise, efficient and highly modular code. Of course a lot depends upon the DI container you choose (picocontainer is by far the lightest of them).
    As regards to the thread safty issues, DI forces you to design for it right from the onset. EJB was designed for the mass programmers (read corporate programmers, better than VBs!) and that's why all that single threadedness and pooling. The vets for ages have always strived to write reentrant code. So largely I see this as an issue depending upon which camp you belong to - EJB or rest of the world!

    Posted by: vijayphadke on March 17, 2005 at 10:37 AM

  • Jesse and Vijay - thanks for the comments. I do want to make it clear that I like DI (I actually prefer interface injection over constructor or setter injection). Just as Vijay points out - DI and EJB architecturally aren't the best mix - especially when we have DL already.

    Posted by: brianrepko on March 17, 2005 at 02:35 PM





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