Javabean 和 EJB 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1361758/
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-08-12 11:10:17  来源:igfitidea点击:

Difference between Javabean and EJB

javaejbjavabeans

提问by LRE

Just a simple question from a relative Java newbie:

只是一个来自相对 Java 新手的简单问题:

what is the difference between a JavaBean and an EJB?

JavaBean 和 EJB 之间有什么区别?

采纳答案by cletus

Java bean is just a set of conventions. EJB is a standard for J2EE business components.

Java bean 只是一组约定。EJB 是 J2EE 业务组件的标准。

Specifically a Java bean:

特别是一个Java bean:

  • has a public default constructor;
  • readable property methods precedes with "get";
  • writable property methods precedes with "set"; and
  • is Serializable.
  • 有一个公共的默认构造函数;
  • 可读的属性方法以“get”开头;
  • 可写属性方法以“set”开头;和
  • 是可序列化的。

For example, a Java bean with a property of "margin" would minimally look like this:

例如,具有“margin”属性的 Java bean 至少如下所示:

public class MyBean implements Serializable {
  private int margin;

  public MyBean() { }
  public int getMargin() { return margin; }
  public void setMargin(int margin) { this.margin = margin; }
}

EJB, despite the name, is almost completely unrelated.

EJB 尽管名称如此,但几乎完全无关。

回答by adatapost

Take a look at this article - JavaBeans vs Enterprise JavaBeans

看看这篇文章 - JavaBeans vs Enterprise JavaBeans

SUMMARY:

JB

JavaBeans takes a low-level approach to developing reusable software components that can be used for building different types of Java applications (applets, stand-alone apps, etc.) in any area.

EJB

Enterprise JavaBeans takes a high-level approach to building distributed systems. It frees the application developer to concentrate on programming only the business logic while removing the need to write all the "plumbing" code that's required in any enterprise application.

概括:

JB

JavaBeans 采用低级方法来开发可重用的软件组件,这些组件可用于在任何领域构建不同类型的 Java 应用程序(小程序、独立应用程序等)。

EJB

Enterprise JavaBeans 采用高级方法来构建分布式系统。它使应用程序开发人员能够专注于仅对业务逻辑进行编程,同时无需编写任何企业应用程序所需的所有“管道”代码。

回答by Charu Jain

  1. JavaBeans may be visible or nonvisible at runtime. For example, the visual GUI component may be a button, list box, graphic or a chart.

    An EJB is a nonvisual, remote object.

  2. JavaBeans are intended to be local to a single process and are primarly intended to run on the client side. Although one can develop server-side JavaBeans, it is far easier to develop them using the EJB specification instead.

    EJB's are remotely executable components or business objects that can be deployed only on the server.

  3. JavaBeans is a component technology to create generic Java components that can be composed together into applets and applications.

    Even though EJB is a component technology, it neither builds upon nor extends the original JavaBean specification.

  4. JavaBeans have an external interface called the properties interface, which allows a builder tool to interpret the functionality of the bean.

    EJBs have a deployment descriptor that describes its functionality to an external builder tool or IDE.

  5. JavaBeans may have BeanInfoclasses, property editors or customizers.

    EJB's have no concept of BeanInfoclasses, property editors or customizers and provide no additional information other than that described in the deployment descriptor.

  6. JavaBeans are not typed.

    EJBs are of two types - session beans and entity beans.

  7. No explicit support exists for transactions in JavaBeans.

    EJB's may be transactional and the EJB servers provide transactional support.

  8. Component bridges are available for JavaBeans. For example, a JavaBean can also be deployed as an Activex control.

    An EJB cannot be deployed as an ActiveX control because ActiveX controls are intended to run at the desktop and EJB's are server side components. However CORBA-IIOP compatibility via the EJB-to-CORBA mapping is defined by the OMG.

  1. JavaBean 在运行时可能可见或不可见。例如,可视化GUI组件可以是按钮、列表框、图形或图表。

    EJB 是一个非可视的远程对象。

  2. JavaBeans 用于单个进程的本地,主要用于在客户端运行。尽管可以开发服务器端 JavaBeans,但使用 EJB 规范来开发它们要容易得多。

    EJB 是只能部署在服务器上的远程可执行组件或业务对象。

  3. JavaBeans 是一种组件技术,用于创建可以组合成小程序和应用程序的通用 Java 组件。

    尽管 EJB 是一种组件技术,但它既不构建也不扩展原始 JavaBean 规范。

  4. JavaBeans 有一个称为属性接口的外部接口,它允许构建器工具解释 bean 的功能。

    EJB 有一个部署描述符,用于向外部构建器工具或 IDE 描述其功能。

  5. JavaBeans 可能有BeanInfo类、属性编辑器或定制器。

    EJB 没有BeanInfo类、属性编辑器或定制器的概念,除了部署描述符中描述的信息外,不提供其他信息。

  6. JavaBeans 没有类型化。

    EJB 有两种类型——会话 bean 和实体 bean。

  7. JavaBeans 中不存在对事务的明确支持。

    EJB 可能是事务性的,EJB 服务器提供事务性支持。

  8. 组件桥可用于 JavaBean。例如,JavaBean 也可以部署为 Activex 控件。

    EJB 不能部署为 ActiveX 控件,因为 ActiveX 控件旨在在桌面上运行,而 EJB 是服务器端组件。然而,通过 EJB 到 CORBA 映射的 CORBA-IIOP 兼容性是由 OMG 定义的。

回答by Vishrant

I found it little cumbersome to understand from the accepted answer so googled few more links and got below answer.

我发现从接受的答案中理解有点麻烦,所以在谷歌上搜索了更多链接并得到了以下答案。

Enterprise JavaBeans (EJB)3.1 these are J2EE specificationswhich instructs server (application server) to deploy a piece of code in EJB Container.

Enterprise JavaBeans (EJB)3.1 这些是J2EE 规范,它指示服务器(应用程序服务器)在EJB 容器中部署一段代码。

EJB technology is the server-side component architecture for the development and deployment of component-based business applications. EJB technology enables rapid and simplified development of distributed, transactional, secure, and portable applications based on Java EE 6 technology.

EJB 技术是用于开发和部署基于组件的业务应用程序的服务器端组件架构。EJB 技术支持基于 Java EE 6 技术的分布式、事务性、安全和可移植应用程序的快速和简化开发。

In simple language:If you create an EJB and deploy it on server, it can be called remotely(using some technique, i.e. JNDI lookup using RMI) or locally(i.e. with in an application).

用简单的语言:如果您创建一个 EJB 并将其部署在服务器上,它可以被远程调用(使用某种技术,即使用 RMI 的 JNDI 查找)或本地调用(即在应用程序中)。

On the other hand, Java beans, is a simple plain Java class with getters and setters and that class is serialized, below is the example:

另一方面,Java beans是一个简单的普通 Java 类,带有 getter 和 setter,并且该类是序列化的,下面是示例:

public class MyBean implements java.io.Serializable
{
   protected  int theValue;

   public MyBean()
   {
   }

   public void setMyValue(int newValue)
   {
      theValue = newValue;
   }

   public int getMyValue()
   {
      return theValue;
   }
}

So, it means there is no comparison between EJB and Java Beans, both are totally different concept.

所以,这意味着 EJB 和 Java Beans 之间没有可比性,两者是完全不同的概念。