7
votes

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

1 Answers

9
votes

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.