Viewscoped JSF and CDI bean

I am using Java EE 6 on jboss eap 6 and my JSF beans is annotated as follows: @ManagedBean @ViewScoped (both from javax.faces.bean package)

However, they are also CDI beans (default constructor, using @Inject @PreDestroy, etc.). I read all the time that you cannot mix these annotations (JSF and CDI), but it seems to work fine: Injections work, preDestroy is called when the view changes, etc.). Am I missing something? What is the problem? Why not use?

+6
source share
1 answer

CDI @Inject works everywhere, and therefore also inside JSF @ManagedBean . The JSF @ManagedProperty parameter only works inside @ManagedBean . You also cannot @Inject support a true JSF bean in any CDI managed bean (it would be a CDI managed instance instead). Perhaps this is what you read about. However, the general consensus is that it is preferable not to mix them in order to avoid confusion between starters. The JSF OmniFaces utility library has CDI @ViewScoped for JSF 2.0 / 2.1.

@PreDestroy , by the way, does not matter, which is typical for CDI, and its analogue @PostConstruct . They should work just fine in both CDI managed beans and JSF beans.

+9
source

Source: https://habr.com/ru/post/954525/


All Articles