Java Reference
In-Depth Information
CDI lets you decouple concerns by what it refers to as loose coupling and strong
typing. In doing so, it provides an almost liberating escape from the banalities of
everyday Java programming, allowing injections of its objects and controlling their
lifetimes.
Tip
Why CDI is for Java EE?
If you have been programming with Java EE 5, you might argue that it already
features resources injection of resources. However, this kind of injection can
be used only for resources known to the container (for example, @EJB , @Per-
sistenceContext , @PersistenceUnit , and @Resource ). CDI, on the other
hand, provides a general-purpose dependency injection scheme, which can be
used for any component.
The CDI elementary unit is still the bean. Compared with EJBs, CDI features a differ-
ent, more flexible kind of bean. One of the most important differences between the
two approaches is that CDI Beans are contextual that is, they live in a well-defined
scope.
Consider the following code snippet:
public class HelloServlet extends HttpServlet {
@EJB EJBSample ejb;
public void doGet (HttpServletRequestreq,
HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println(ejb.greet());
out.close();
}
}
Search WWH ::




Custom Search