简体   繁体   English

Viewscoped JSF和CDI bean

[英]Viewscoped JSF and CDI bean

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) 我在JBoss EAP 6上使用Java EE 6,我的JSF bean注释如下: @ManagedBean @ViewScoped (均来自javax.faces.bean包)

However, they are also CDI beans (default constructor, use of @Inject , @PreDestroy etc). 但是,它们也是CDI bean(默认构造函数,使用@ @Inject ,@ @PreDestroy等)。 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). 我一直在阅读你不能混合这些注释(JSF和CDI),但它显然工作正常:注入正在工作,preDestroy在视图更改时被调用等)。

Am I missing something? 我错过了什么吗? What is the problem? 问题是什么? Why not use? 为什么不用?

The CDI @Inject works "everywhere" and thus also inside JSF @ManagedBean . CDI @Inject “无处不在”,因此也在JSF @ManagedBean The JSF counterpart @ManagedProperty works inside @ManagedBean only. JSF的对应@ManagedProperty作品里面@ManagedBean只。 You also can't @Inject a true JSF managed bean in any CDI managed bean (instead, it would be a CDI managed instance). 你也不能在任何CDI托管bean中@Inject一个真正的JSF托管bean(相反,它将是一个CDI托管实例)。 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. JSF实用程序库OmniFaces与JSF 2.0 / 2.1具有CDI兼容的@ViewScoped

The @PreDestroy is by the way not specific to CDI, neither is its counterpart @PostConstruct . @PreDestroy 不是特定于CDI的方式,也不是它的对应@PostConstruct They should work just fine in both CDI managed beans and JSF managed beans. 它们应该在CDI托管bean和JSF托管bean中都能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM