将JSF管理Bean注入EJB [英] Injecting JSF Managed Bean to a EJB

查看:189
本文介绍了将JSF管理Bean注入EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将JSF Managed Bean注入到EJB中吗?我已经将JSF Managed Bean注入到另一个JSF Managed Bean中作为@ManagedProperty。但是当我对EJB做同样的事情时,我得到一个空点异常。

解决方案

不,这是不可能的。 @ManagedProperty 仅适用于 @ManagedBean 类(JSF托管bean)。您只能使用 @EJB @Inject 注入另一个EJB或CDI托管bean($ @Named 类)。



然而,在像EJB这样的业务服务类中,将前端类(如JSF或CDI托管的bean)插入到业务服务类中,使设计技术上没有任何意义。应该设计一个EJB,使其无变化可以重复使用在一个完全不同的前端,如JAX-RS webservice,甚至是一个简单的vanilla servlet。 EJB绝对不应该有任何 javax.faces。* import / dependencies(就像它不应该有任何 javax.ws.rs。* javax.servlet。* one)。



如果您打算传递数据从JSF托管的bean到一个EJB,然后将它作为method参数传递。这样的数据通常是JPA @Entity 或至少一个返回一个实体的ID /关键字的风格。


Is is possible to inject JSF Managed Bean into an EJB? I have injected JSF Managed Beans in to another JSF Managed Bean as a @ManagedProperty. But when I do the same to the EJB, I get a null point exception.

解决方案

No, that's not possible. The @ManagedProperty works inside @ManagedBean classes (JSF managed beans) only. You can only use @EJB or @Inject to inject another EJB or a CDI managed bean (a @Named class).

However, it makes design technically no sense to inject a front-end class like a JSF or CDI managed bean in a business service class like an EJB. An EJB should be designed in such way that it can without changes be reused together a completely different front-end like JAX-RS webservice or even a plain vanilla servlet. An EJB should absolutely not have any javax.faces.* imports/dependencies (like as that it should not have any javax.ws.rs.* nor javax.servlet.* ones).

If you intend to pass data from the JSF managed bean to an EJB, then just pass it as method argument. Such data is usually in flavor of a JPA @Entity or at least an ID/keyword which returns an entity.

这篇关于将JSF管理Bean注入EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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