JSF Tip #54 - Enable CDI when bundling JSF

Occasionally, users have the need to override the version of JSF included in the application server by bundling a different version of JSF with their application. If you are trying to use CDI in such a scenario, you might have noticed it does not work. Is there a way out?

Yes, there is, by including a small JAR and some configuration in your web application you can make this scenario work.

Add the following Maven dependency to your web application.


    <dependency>
        <groupId>com.oracle.cdi-enabler</groupId>
        <artifactId>cdi-enabler-1_0</artifactId>
        <version>1</version>
        <scope>compile</scope>
    </dependency>
        

Then add the following beans.xml to your WEB-INF directory and you are set to go!


    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
        <alternatives>
            <class>com.oracle.cdi_enabler.CdiConversation</class>
        </alternatives>
    </beans>
        

Note this particular version of the JAR requires you to use this on a JavaEE6 container that bundles a CDI 1.0 implementation, like Glassfish 3.1.2.2 or Weblogic 12.1.2.

Posted December 10, 2013

Up