Skip to Main Content
Java EE 6 Pocket Guide
book

Java EE 6 Pocket Guide

by Arun Gupta
September 2012
Intermediate to advanced content levelIntermediate to advanced
210 pages
3h 59m
English
O'Reilly Media, Inc.
Content preview from Java EE 6 Pocket Guide

Chapter 6. Contexts and Dependency Injection

Contexts and Dependency Injection (CDI) is defined as JSR 299, and the complete specification can be downloaded from http://jcp.org/aboutJava/communityprocess/final/jsr299/index.html.

CDI defines a type-safe dependency injection mechanism in the Java EE platform. A bean specifies only the type and semantics of other beans it depends upon, without a string name and using the type information available in the Java object model. This allows compile-time validation in addition to deployment. It also provides for easy refactoring.

The injection request need not be aware of the actual lifecycle, concrete implementation, threading model, or other clients of the bean. This “strong typing, loose coupling” makes your code easier to maintain. The bean so injected has a well-defined lifecycle and is bound to lifecycle contexts. The injected bean is also called a contextual instance because it is always injected in a context.

Almost any POJO can be injected as a CDI bean. This includes EJBs, JNDI resources, entity classes, and persistence units and contexts. Even the objects that were earlier created by a factory method can now be easily injected. Specifically, CDI allows EJB components to be used as JSF managed beans, thus bridging the gap between the transactional and the web tier. It is also integrated with Unified Expression Language (UEL), allowing any contextual object to be used directly within a JSF or JSP page.

Injection Points

A bean may be injected ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java EE 7 Essentials

Java EE 7 Essentials

Arun Gupta
Beginning Java EE 7

Beginning Java EE 7

Antonio Goncalves
The Java EE 6 Tutorial: Advanced Topics, Fourth Edition

The Java EE 6 Tutorial: Advanced Topics, Fourth Edition

Eric Jendrock, Ricardo Cervera-Navarro, Ian Evans, Devika Gollapudi, Kim Haase, William Markito, Chinmayee Srivathsa

Publisher Resources

ISBN: 9781449338329Errata