Viewscoped JSF and CDI bean

廉价感情. 提交于 2020-01-23 05:42:25

问题


I'm using Java EE 6 on JBoss EAP 6, and my JSF beans are annotated like this: @ManagedBean @ViewScoped (both from javax.faces.bean package)

However, they are also CDI beans (default constructor, use of @Inject, @PreDestroy etc). I'm reading all the time that you can't mix these annotations (JSF and CDI), but it's apparently working fine: Injections are working, preDestroy is called on view change etc).

Am I missing something? What is the problem? Why not use?


回答1:


The CDI @Inject works "everywhere" and thus also inside JSF @ManagedBean. The JSF counterpart @ManagedProperty works inside @ManagedBean only. You also can't @Inject a true JSF managed bean in any CDI managed bean (instead, it would be a CDI managed instance). Perhaps this is what you was reading about. General consensus, however, is indeed to preferably not mix them to avoid confusion among starters. JSF utility library OmniFaces has a CDI compatible @ViewScoped for JSF 2.0/2.1.

The @PreDestroy is by the way not specific to CDI, neither is its counterpart @PostConstruct. They should work just fine in both CDI managed beans and JSF managed beans.



来源:https://stackoverflow.com/questions/18988276/viewscoped-jsf-and-cdi-bean

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