Viewscoped JSF和CDI bean [英] Viewscoped JSF and CDI bean

查看:122
本文介绍了Viewscoped JSF和CDI bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是,它们也是CDI bean(默认构造函数,使用@Inject@PreDestroy等).我一直在阅读您无法混合使用这些批注(JSF和CDI)的消息,但显然工作得很好:注入正常,在视图更改时调用preDestroy等).

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?

推荐答案

CDI @Inject可以在任何地方"工作,因此也可以在JSF @ManagedBean内部工作.与JSF对应的@ManagedProperty仅在@ManagedBean内部工作.您也不能在任何CDI托管Bean中@Inject真正的JSF托管Bean(相反,它将是CDI托管实例).也许这就是您正在阅读的内容.但是,普遍的共识实际上是最好不要将它们混合在一起,以避免启动程序之间的混淆. JSF实用程序OmniFaces 具有与CDI兼容的 @ViewScoped for JSF 2.0/2.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.

@PreDestroy特定于CDI的方式,它的对应方@PostConstruct也不是.它们在CDI托管Bean和JSF托管Bean中都应该可以正常工作.

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.

这篇关于Viewscoped JSF和CDI bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆