7

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?

bruno
  • 2,213
  • 1
  • 19
  • 31
htft
  • 113
  • 1
  • 3

1 Answers1

9

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for the answer. Seems I'm really confused about all these things... So what I have is a JSF managed bean, rather than a CDI bean? Even if this bean meets all requirements do be a CDI bean documentation talks about (default constructor, not inner class etc)? If so, what the difference to a CDI bean? what are the drawbacks or what I can't do with this bean that I can do with a CDI bean? – htft Sep 24 '13 at 18:46
  • You can't `@Inject` the JSF managed bean instance elsewhere. If you don't actually need it, then there's not really a drawback. But noted should be that JSF is slowly moving to CDI. Upcoming new JSF specific annotations like `@FlowScoped` are CDI-only. The JSF `@ManagedBean` facility will be deprecated in near future. Users are encouraged to move to CDI before it happens. – BalusC Sep 24 '13 at 18:52