- Java EE 8 and Angular
- Prashant Padmanabhan
- 481字
- 2021-07-02 19:22:34
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)
- Spring Cloud Alibaba核心技術與實戰案例
- Java EE 6 企業級應用開發教程
- Learning Chef
- 編程珠璣(續)
- OpenCV with Python By Example
- Developing SSRS Reports for Dynamics AX
- Python:Deeper Insights into Machine Learning
- Machine Learning With Go
- Flask Web開發:基于Python的Web應用開發實戰(第2版)
- Photoshop智能手機APP界面設計
- 算法秘籍
- 網絡綜合布線與組網實戰指南
- Android熱門應用開發詳解
- Python程序設計教程
- 軟件自動化測試實戰解析:基于Python3編程語言