Java Reference
In-Depth Information
CDI can work with any Plain Old Java Object (POJO) by instantiating and injecting objects into
each other. The following types of objects are injectable:
Enterprise resources such as data source and queues
Remote EJB references
Session beans
EntityManager objects
Web services references
Producer i elds and objects returned by producer methods
CDI Versus EJB
Although CDI and EJB seem to be rivals, they work in harmony. CDI can work alone without an
EJB container. Actually, CDI can power a desktop application or any web application that doesn't
rely on the EJB container. CDI provides the factory and injection to any JavaBean.
However, EJBs still require the EJB container. Even the simplii ed architecture of EJBs is more
complex than POJOs, so EJBs still need the EJB container. The EJB container provides additional
services such as security, transactions, and concurrency that EJBs need.
Simply put, the CDI container is a lighter, powerful, but less functional container for POJOs. Still,
both containers are so well integrated that CDI annotations can act as a gateway and standard
interface to interact with the EJB container. For example, the @Inject annotation can work with
either POJOs or EJBs and can inject any combination of each by invoking the right container to
handle the job.
CDI Beans
A container managed bean is little more than a POJO that conforms to some simple rules:
It must have a no‐argument constructor, or the constructor must declare an @Inject
The class must be a top‐level concrete class or must be annotated with @Decorate ; it cannot
be a nonstatic inner class.
It cannot be dei ned as an EJB.
If the bean is dei ned as a managed bean by another Java EE technology, such as the JSF
technology, it will also be managed by the container.
Any class conforming to these requirements will be instantiated and managed by the container and
is injectable. No special annotation is required to dei ne the class as a managed bean.
The container looks for bean‐inside‐bean archives. There are two types of bean archives: explicit
and implicit. An explicit archive contains a bean.xml deployment descriptor, which is normally
Search WWH ::

Custom Search