Skip to main content

JSF Tip #54 - Enable CDI when bundling JSF

Posted by mriem on December 10, 2013 at 3:46 AM PST

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.

And that is it.

Enjoy!

Related Topics >>

Comments

With this configuration the application works fine in WAS ...

With this configuration the application works fine in WAS 8.5.5 but CDI enabler attaches CID numbers to the first form in JSF page.
Is there any way to solve this?

Can you ask for support in the ...

Can you ask for support in the users@javaserverfaces.java.net? Thanks!

Wow. This actually seems to work in WebSphere Application ...

Wow. This actually seems to work in WebSphere Application Server v8.0!

The only difference is that WAS 8 complained about the beans.xml modification. The WAS log stated that the alternative was already enabled.

I also put the myfaces-api and myfaces-impl dependencies before the javaee-api dependency in my pom and set the classloader policy to PARENT_LAST.

Thanks, Manfred!