Usage of CDI in Oracle ADF - Part II - Enabling and using CDI

Hi,
I will bring some serie of posts about CDI usage within Oracle ADF:
Previously I have introduced Usage of CDI in Oracle ADF - Part I - Introduction. what are CDI Beans in Java EE 8 and why are them so important in Java Web Development.

Now, we are going to setup our ADF Project to be able to use CDI Beans.


How do I enable CDI in an ADF Application?

Important Update: Do not include CDI 2.0 or other Java Enterprise dependencies of higher versions that are shipped with Oracle WebLogic Server.

E.g.: Oracle WebLogic 12.2.1.3 is CDI 1.1. compatible

If you are using Maven or the classic JDeveloper Library manageemnt then add the Context and Dependency Injection library to your pom.xml



Also, you can add manually the CDI dependency as following:


            com.oracle.adf.library
            Contexts-and-Dependency-Injection
            12.2.1-3-0
            pom
            provided
        

Is it enough? No!!!, we need to create the beans.xml file.



So, not we are ready to use CDI Managed Beans and use the context and dependency injection from classes of other libraries!.

Remember, when using annotations such as @RequestScoped you must import the javax.enterprise.context.RequestScoped instead of the javax.faces.beans

You can find the code of this post in: https://github.com/DanielMerchan/adf


Test Cases


I have created the following classes:
  • CDI: RequestCDIBean, SessionCDIBean and ApplicationCDIBean are CDI Managed Beans

  • ADF: RequestJSFBean, SessionJSFBean and ApplicationJSFBean are ADF Managed Beans registered in adfc-config.xml and managed by ADF.

  • JSF: RequestJSFAnnBean and ApplicationJSFAnnBean are JSF Managed Beans registered using annotations. 

The RequestXXXBean are invoked from a button where you can test to @Inject or use @ManagedProperty annotation of JSF 2.2 to re-use Beans defined. 

As far as I remember the results of the research was:
  1. CDI Managed Bean could inject by using @Inject annotation all CDIs Beans (as expected) and... "JSF / ADF Managed Beans"?? Care and read below:

    However it is confusing!: By using @Inject in the attributes of type JSF / ADF Managed Bean were NOT really Injecting the Managed Bean defined using the JSF Scope Annotations. If you want to inject a JSF Managed Bean into a CDI Managed Bean you need to use the @ManagedProperty which is CDI compatible available in JSF 2.3+.

  2. JSF Managed Beans using annotations and managed by JSF 2.2 were able to @Inject the CDI Managed Beans by using the annotation, and @Inject the JSF / ADF Managed Beans by using @ManagedProperty annotation.

  3. ADF Managed Beans registered in adfc-config.xml could not use CDI Managed Beans (NullPointer) and cannot inject the ManageProperty using annotations.

Filling the gaps... workarounds and tips

  1.  If you have your Managed Beans defined in ADF configuration files such as adfc-config.xml or task-flow-definition.xml you cannot use @Inject for injecting CDI Managed Beans into them.

    Use OmniFaces 2.7 Beans Utility to get CDI Managed Beans inside of them:
    http://showcase.omnifaces.org/utils/Beans

    In the @PostConstruct method, use the Beans.getReference(YourCDIBean.class)
        /**
         * Post Construct which ensures the context-dependency injection is ready.
         */
        @PostConstruct
        public void init() {
            System.out.println("RequestJSFBean initiated");        
            // Get the CDI Managed Beans by using OmniFaces 2.7
            appCDIBean = Beans.getReference(ApplicationCDIBean.class);
        }
  2. Do NOT try to use ADF / JSF Managed Beans defined in adfc-config.xml / task-flow-definition.xml file in CDI Managed Beans.

Comments

Popular posts from this blog

OJET: Inter-Module communication in TypeScript Template

OJET: Build and Deploy in an Application Server

OJET: Select All options using only Checkboxset