5 Dependency Injection and CDI

WHAT’S IN THIS CHAPTER?            

  • Introduction to dependency injection
  • Why DI is important in Java EE
  • How to implement DI in plain code
  • How DI is implemented in Java EE
  • Introduction to Content Dependency Injection
  • Key differences between CDI and EJB containers

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code download for this chapter is found at www.wrox.com/go/projavaeedesignpatterns on the Download Code tab. The code is in the Chapter 5 download and individually named according to the names throughout the chapter.

Dependency injection (DI) is one of the few well-known and accepted design patterns that was not listed in the book by the Gang of Four.1 Today, it has been used widely in modern programming languages both internally and as a best practice to promote loose coupling.

J2EE was designed to handle the most complex systems but failed miserably by overcomplicating the development of even the simpler systems. The original design of J2EE relied on heavyweight complexity and tight coupling, which led to the popularity of frameworks such as Spring and Pico container. In 2004, Martin Fowler published an article on the inversion of control containers and the dependency of the injection pattern.2 Most vendors did not support and encourage developers to use the J2EE container. However, soon the lightweight containers took over, they became officially supported and, even more, Spring became the unofficial de facto standard and led ...

Get Professional Java EE Design Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.