Search code examples
jsfjakarta-eejsf-2cdimanaged-bean

Can CDI managed beans and JSF managed beans talk to each other?


I have a Tomcat 6 JSF web application that I'd like to set up with CDI beans. I will have to convert the project to CDI gradually though.

My question is: can CDI beans and traditional JSF managed beans be injected into each other?

Thanks.


Solution

  • All JSF managed beans (JMB) either are CDI managed beans (CMB) automatically, or can be recognized as such using the beans.xml marker file. (The requirements of a CMB are set very low and basically just dictate the existence of a non-parameter constructor.)

    This means that you should be able to use (read: inject) all JMBs with almost no extra effort.

    However, problems will arise because JMBs use different scopes than CMBs (javax.faces.* vs. javax.enterprise.*)

    These are best solved by registering a relative simple CDI-extension: When the CDI server (Tomcat for you) starts, it will raise a couple of events that you can observe - this is be the place where you can detect JMBs and convert them into correctly scoped CMBs.

    But before you start hacking: All this has already been done by the Seam 3 Faces module. Simply put it in your classpath, and all JSF-scopes are bridged to CDI-scopes.