Usage of CDI in Oracle ADF - Part I - Introduction
Hi,
I will bring some serie of posts about CDI usage within Oracle ADF:
In JSF, JSF Managed Beans are defined in the faces-config.xml and managed by the JSF Framework.
Remember that Annotations for JSF Managed Beans were introduced in JSF 2.0 allowing us to declare the JSF Managed Beans within the class itself instead of registering in a faces config file.
Note: The JSF annotations are located under javax.faces.beans package. However, they have been deprecated in JSF 2.3 and possibily in the future they will be removed.
So the recommendation is to use CDI beans in JSF world!.
As you can see, ViewScoped and FlowScoped remains in a separate faces package as they are special for Java Server Faces Development. But, they are 100% CDI compatible.
In addition, I have omitted some pseudo-scopes like Dependant or Singleton. You can find more and better information online of them!
Yes, but with limitations or workarounds in some cases:
I will bring some serie of posts about CDI usage within Oracle ADF:
- Usage of CDI in Oracle ADF - Part I - Introduction.
- Usage of CDI in Oracle ADF - Part II - Enabling and using CDI.
- Usage of CDI in Oracle ADF - Part III - CDI Events - Synchronous
Some history on JSF and Managed Beans
Looooong time ago... JSF as other frameworks introduced a way on how the objects are created, injected and used without the need of making explicit new in our code for memory optimisation etc...In JSF, JSF Managed Beans are defined in the faces-config.xml and managed by the JSF Framework.
Remember that Annotations for JSF Managed Beans were introduced in JSF 2.0 allowing us to declare the JSF Managed Beans within the class itself instead of registering in a faces config file.
Note: The JSF annotations are located under javax.faces.beans package. However, they have been deprecated in JSF 2.3 and possibily in the future they will be removed.
So the recommendation is to use CDI beans in JSF world!.
CDI vs JSF Managed Beans. Why CDI?
Just some comments:- CDI allows a complete Java EE / Jakarta EE-wide application injection (included POJOs) where JSF only allows a subset in terms of injection.
- JSF Beans are managed by JSF and CDI is managed by the app container.
- JSF Managed Beans are deprecated since JSF 2.3. So they will be removed!. So CDI will be the only one to manage and decide when to create and destroy Managed Beans.
CDI Managed Beans vs ADF Managed Beans
Those are the Scopes available in CDI and ADF.
ADF
Managed Bean Scope
|
CDI
Scope
|
Common
used for…
|
Backing Bean Scope
|
-
|
Usually used within in Oracle ADF for managing UI
Components, Events and Actions that can be taken in a specific fragment or
screen.
There is not Backing Bean equivalent in CDI. |
Request Scope
|
RequestScoped
(javax.enterprise.context)
|
Uses in ADF for a Managed Bean for a single request but can be shared
by multiple Task Flow or components within the same request (little bit wider
than Backing Bean Scope).
In CDI used for Managed Beans which are not required to persist
longer than a request.
|
View Scope
|
ViewScoped
(java.faces.view)
|
Used to keep the information within the same view
across multiple requests.
The ADF View Scope and the CDI ViewScoped are different. An ADF View Scope can be scoped to a specific fragment of af:region (which is not accessible from the parent page). |
Page Flow Scope
|
FlowScoped
(javax.faces.flow)
|
Used to keep the information during a flow of fragments / pages.
The ADF Page Flow Scope is for being used in ADF Task Flows and the JSF Flow Scope and not compatible as is intented to be used within JSF Page Flows. |
Session Scope
|
SessionScoped
(javax.enterprise.context)
|
Used to keep beans / information alive within all
the user session.
|
Application Scope
|
ApplicationScoped
(javax.enterprise.context)
|
Used to keep beans alive for the entire application and shared by
multiple sessions.
|
-
|
ConversationScoped
(javax.enterprise.context)
|
A developer can start / destroy a Managed Bean in ConversationScoped.
For example if a Managed Bean is only needed for a certain steps of the JSF Lifecycle |
As you can see, ViewScoped and FlowScoped remains in a separate faces package as they are special for Java Server Faces Development. But, they are 100% CDI compatible.
In addition, I have omitted some pseudo-scopes like Dependant or Singleton. You can find more and better information online of them!
The question is... I am in ADF... Can I use CDI in Oracle ADF?
Yes, but with limitations or workarounds in some cases:
- Oracle ADF still uses its own Page Flow Scope definition which differs from the FlowScoped of CDI. So the Flow Scoped scope and the FlowMap.
- Oracle ADF defines its own ViewScope which differs from the ViewScoped of CDI.
- Injection. In Oracle ADF you cannot use the ManagedProperty CDI compatible implementation due to is included as part of JSF 2.3+. If you want to @Inject a CDI in an ADF Managed Bean you can use OmniFaces Beans utility to get the CDI Bean!. http://showcase.omnifaces.org/utils/Beans
- CDI version: Oracle WebLogic 12.2.1.3 is shipped with CDI 1.1, so 1.2 and 2.0 capabilities such as Asynchronous events are not supported!.
I am so grateful for your article.
ReplyDeleteoracle adf training
oracle adf online training