java jsf bean 和 ejb bean 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4295303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:44:12  来源:igfitidea点击:

difference between jsf beans and ejb beans

javajsfjakarta-eeejb

提问by Alexis

I need some clarification. I know how to work with JSF and its corresponding session beans, but i am getting confused with EJB. What is the difference between the beans introduced with EJB and the session beans used with JSF (for ejb i know about the stateless/full session beans and entity beans, entity manager, etc.). What i just dont get is when to use EJB and when to use jsf beans. Aside from the entity beans, both the ejb stateful/less session beans seem similar to the jsf session beans. I've read about injection ejb's into jsf, but why not just use ejb in conjunction with jsf beans? I hope you can understand my confusion. Thank you.

我需要澄清一下。我知道如何使用 JSF 及其相应的会话 bean,但我对 EJB 感到困惑。EJB 引入的 bean 和 JSF 使用的会话 bean 之间有什么区别(对于 ejb,我知道无状态/完整会话 bean 和实体 bean、实体管理器等)。我只是不明白什么时候使用 EJB,什么时候使用 jsf bean。除了实体 bean,ejb 有状态/无状态会话 bean 看起来都类似于 jsf 会话 bean。我已经阅读了将 ejb 注入 jsf 的内容,但为什么不将 ejb 与 jsf bean 结合使用?我希望你能理解我的困惑。谢谢你。

回答by Alexis

First of all, we need to know about the difference between JSFand EJBbeans.

首先,我们需要了解JSFEJBbean的区别。

  1. JSF beansare POJO classeswhich used to read the component value of JSF. There are two type of beans in JSF:

    • Managed beanis about how a java bean is created and initialized. As you know, JSF uses the Lazy initialization model. It means that the bean in the particular scope is created and initializednot at the moment when the scope is started, but on-demand, i.e. when the bean is first time required.

    • Backing beanis about the role a particular managedbean plays. This is a role to be a server-side representation of the components located on the page. Usually, the backing beans have a request scope, but it is not a restriction.

  2. EJB Beanis a server-side component that encapsulates the business logicof an application. The business logic is the code that fulfills the purpose of the application.

  1. JSF豆POJO classes其用于读取JSF的成分值。JSF 中有两种类型的 bean:

    • 托管 bean是关于如何创建和初始化 java bean。如您所知,JSF 使用 Lazy 初始化模型。这意味着特定作用域中的 bean不是在作用域启动时创建和初始化的,而是按需创建和初始化的,即在第一次需要 bean 时。

    • 支持 bean是关于特定托管bean 所扮演的角色。这是作为位于页面上的组件的服务器端表示的角色。通常,支持 bean 具有请求范围,但这不是限制。

  2. EJB Bean封装应用程序业务逻辑的服务器端组件。业务逻辑是实现应用程序目的的代码。

Mainly, there are three types of session beans:

主要有三种类型的会话bean:

1.Statefullsession bean

1.有状态的会话bean

2.Statelesssession bean

2.无状态会话bean

3.Singletonsession bean(ejb 3.1)

3.单例会话 bean(ejb 3.1)

回答by Arjan Tijms

There is indeed some confusion between the different types of managed beans in Java EE. To add to the confusion, Java EE 6 has introduced a third kind of managed bean: a CDI bean.

Java EE 中不同类型的托管 bean 之间确实存在一些混淆。更令人困惑的是,Java EE 6 引入了第三种托管 bean:CDI bean。

In this answer I try to explain the differences and similarities a little: How do CDI and EJB compare? interact?

在这个答案中,我尝试稍微解释一下差异和相似之处:CDI 和 EJB 如何比较?相互作用?

Briefly said, JSF managed beans mainly don't offer support for transactions, which is something you often need when working with business logic and especially JPA.

简而言之,JSF 托管 bean 主要不提供对事务的支持,这是您在处理业务逻辑尤其是 JPA 时经常需要的。

Also note that the term sessionas in session scoped managed beans is a completely different kind of session than the one the term in stateless and statefull session beans refers to.

另请注意,session会话范围托管 bean 中的术语与无状态和有状态会话 bean 中的术语完全不同。

回答by Croo

There is another gread answer on the site where the differences of CDI and EJB are explained. It helps a great deal when you finally grasp the whole picture. Where to use EJB 3.1 and CDI?

该站点上还有另一个很好的答案,其中解释了 CDI 和 EJB 的差异。当您最终掌握整个画面时,它会很有帮助。在哪里使用 EJB 3.1 和 CDI?