官术网_书友最值得收藏!

CDI programming model

This programming model unifies the various approaches taken by existing dependency injection frameworks such as Spring, Guice, and Seam. It has been designed considering the scope of objects as well. CDI was introduced in Java EE 6, and now with Java EE 8, we have the latest CDI 2.0 version, which offers Java SE support as well. While Java EE 5 did make resource injection possible, it did not have the support for general purpose dependency injection for objects. With CDI, we have beans that are container managed, and these managed beans are Plain Old Java Objects (POJOs) whose life cycle is controlled by the Java EE container. The idea of the bean has been presented in Java for ages in various forms, such as JSF bean, EJB bean, and so on. CDI takes this idea and provides a unified view to managed beans across the entire Java platform, which is relied on by every other specification including JSF, EJB, Servlet, JPA, and more. This allows you to take advantage of this specification together with the services offered by other Java EE specifications. While writing a trivial application, you may not find the benefits of DI appealing enough, but the moment your code grows, as well as its dependencies, it can certainly benefit from using CDI.

While CDI can be used in both SE and EE environments, there are few considerations to be noted for its usage in the Java EE environment, which is component-based. CDI enhances the EJB model by providing contextual life cycle management. While session beans can be injected into other beans, the same can’t be said for message driven Beans and entity beans as they are considered non-contextual objects. Since CDI allows for defining any bean with a name, this feature allows you to reference the bean in JSF applications as well.

This specification defines five built-in scopes, which are:

  • RequestScoped
  • SessionScoped
  • ApplicationScoped
  • ConversationScoped
  • DependentScoped

It's also possible to define custom scopes if needed using portable extensions. Apart from contextual scopes, we get a flexible event notification model and the ability to use interceptors for cross-cutting concerns and decorators.

It’s not possible to cover all of what CDI has to offer in a single chapter, but we will go through enough features to be proficient in utilizing CDI to our advantage. Those wanting to explore further can do so by looking up the references mentioned later ahead.

A few of the services offered by CDI are listed here:

  • Type-safe dependency injection with the ability to swap a dependency for another during deployment using metadata placed in XML
  • EL expression integration to allow for resolving beans in JSF or JSP pages.
  • Addition of interceptor bindings and decorators
  • Powerful event notification model which makes use of the observer pattern
  • Ability to extend CDI with portable extensions using its Service Provider Interface (SPI)
主站蜘蛛池模板: 高碑店市| 昂仁县| 张家界市| 贡觉县| 沭阳县| 涟源市| 晋城| 邵阳县| 马关县| 永顺县| 合阳县| 静宁县| 花莲市| 金塔县| 洱源县| 昭通市| 开阳县| 仪陇县| 都匀市| 株洲县| 文登市| 浦东新区| 文成县| 饶阳县| 永济市| 灵台县| 蕲春县| 定远县| 黑龙江省| 镇沅| 吴忠市| 松潘县| 栖霞市| 焉耆| 耒阳市| 成武县| 双江| 云南省| 德州市| 宁蒗| 肃南|