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

烂漫一生 提交于 2019-12-18 16:57:41

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7734214/can-cdi-managed-beans-and-jsf-managed-beans-talk-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!