Tuesday, January 17, 2012

JavaEE 6: What is CDI?

Contexts and Dependency Injection, more commonly known as CDI, is one of the new features for the Java EE Platform. It allows us to bind together the web tier and transactional tier of Java EE platform.

The two most fundamental services provided by CDI are as follows:

  • Contexts: The ability to bind the lifecycle and interactions of stateful components to well-defined but extensible lifecycle contexts
  • Dependency injection: The ability to inject components into an application in a type-safe way, including the ability to choose at deployment time which implementation of a particular interface to inject.

In addition, CDI provides a rich programming model through loose-coupling with strong typing. A bean need not be aware of the actual lifecycle, concrete implementation, threading model or other clients of any bean it interacts with. Events, interceptors and decorators enhance this loose-coupling:

  • event notifications decouple event producers from event consumers,
  • interceptors decouple technical concerns from business logic, and
  • decorators allow business concerns to be compartmentalized.

Along with loose coupling, CDI provides strong typing, as follows:
  • CDI eliminates lookup using string-based names for wiring and correlations, so that the
    compiler will detect typing errors.
  • It allows the use of declarative Java annotations to specify everything, largely eliminating the need for XML deployment descriptors, and making it easy to provide tools that introspect
    the code and understand the dependency structure at development time.

No comments: