JavaBean 和 POJO 之间有什么区别?

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

What is the difference between a JavaBean and a POJO?

javaterminologypojo

提问by DJphilomath

I'm not sure about the difference. I'm using Hibernate and, in some books, they use JavaBean and POJO as an interchangeable term. I want to know if there is a difference, not just in the Hibernate context, but as general concepts.

我不确定有什么区别。我正在使用 Hibernate,并且在某些书中,他们使用 JavaBean 和 POJO 作为可互换的术语。我想知道是否有区别,不仅在 Hibernate 上下文中,而且在一般概念中。

采纳答案by Brian Agnew

A JavaBean follows certain conventions. Getter/setter naming, having a public default constructor, being serialisable etc. See JavaBeans Conventionsfor more details.

JavaBean 遵循某些约定。Getter/setter 命名、具有公共默认构造函数、可序列化等。有关更多详细信息,请参阅JavaBeans 约定

A POJO (plain-old-Java-object) isn't rigorously defined. It's a Java object that doesn't have a requirement to implement a particular interface or derive from a particular base class, or make use of particular annotations in order to be compatible with a given framework, and can be any arbitrary (often relatively simple) Java object.

POJO(plain-old-Java-object)没有严格定义。它是一个 Java 对象,不需要实现特定的接口或派生自特定的基类,也不需要使用特定的注释来与给定的框架兼容,并且可以是任意的(通常相对简单) Java 对象。

回答by nagapavan

All JavaBeans are POJOs but not all POJOs are JavaBeans.

所有 JavaBean 都是 POJO,但并非所有 POJO 都是 JavaBean。

A JavaBean is a Java object that satisfies certain programming conventions:

JavaBean 是满足某些编程约定的 Java 对象:

  • the JavaBean class must implement either Serializable or Externalizable;
  • the JavaBean class must have a public no-arg constructor;
  • all JavaBean properties must have public setter and getter methods (as appropriate);
  • all JavaBean instance variables should be private.
  • JavaBean 类必须实现 Serializable 或 Externalizable;
  • JavaBean 类必须有一个公共的无参数构造函数;
  • 所有 JavaBean 属性都必须具有公共 setter 和 getter 方法(视情况而定);
  • 所有 JavaBean 实例变量都应该是私有的。

回答by Sphygmomanometer

According to Martin Fowler a POJO is an object which encapsulates Business Logic while a Bean (except for the definition already stated in other answers) is little more than a container for holding data and the operations available on the object merely set and get data.

根据 Martin Fowler 的说法,POJO 是一个封装业务逻辑的对象,而 Bean(除了其他答案中已经说明的定义)只不过是一个用于保存数据的容器,对象上可用的操作只是设置和获取数据。

The term was coined while Rebecca Parsons, Josh MacKenzie and I were preparing for a talk at a conference in September 2000. In the talk we were pointing out the many benefits of encoding business logic into regular java objects rather than using Entity Beans. We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely.

http://www.martinfowler.com/bliki/POJO.html

这个术语是在 Rebecca Parsons、Josh MacKenzie 和我准备在 2000 年 9 月的一次会议上进行演讲时创造的。在演讲中,我们指出了将业务逻辑编码到常规 Java 对象而不是使用实体 Bean 的许多好处。我们想知道为什么人们如此反对在他们的系统中使用常规对象并得出结论,这是因为简单的对象缺乏一个花哨的名字。所以我们给了他们一个,它很受欢迎。

http://www.martinfowler.com/bliki/POJO.html

回答by Sphygmomanometer

POJOSwith certain conventions (getter/setter,public no-arg constructor ,private variables) and are in action(ex. being used for reading data by form) are JAVABEANS.

POJOS具有某些约定(getter/setter、公共无参数构造函数、私有变量)并且正在运行(例如用于按表单读取数据)是JAVABEANS.

回答by prasad paluru

POJO: If the class can be executed with underlying JDK,without any other external third party libraries support then its called POJO

POJO:如果该类可以使用底层JDK执行,而无需任何其他外部第三方库支持,则称为POJO

JavaBean: If class only contains attributes with accessors(setters and getters) those are called javabeans.Java beans generally will not contain any bussiness logic rather those are used for holding some data in it.

JavaBean:如果类只包含带有访问器(setter 和 getter)的属性,则这些属性称为 javabeans。Java bean 通常不包含任何业务逻辑,而是用于在其中保存一些数据。

All Javabeans are POJOs but all POJO are not Javabeans

所有 Javabean 都是 POJO,但所有 POJO 都不是 Javabean

回答by Mandeep Gill

Pojo - Plain old java object

Pojo - 普通的旧 Java 对象

pojo class is an ordinary class without any specialties,class totally loosely coupled from technology/framework.the class does not implements from technology/framework and does not extends from technology/framework api that class is called pojo class.

pojo 类是一个没有任何特长的普通类,与技术/框架完全松散耦合的类。该类不是从技术/框架实现的,也不是从技术/框架 api 扩展的,该类称为 pojo 类。

pojo class can implements interfaces and extend classes but the super class or interface should not be an technology/framework.

pojo 类可以实现接口和扩展类,但超类或接口不应是技术/框架。

Examples :

例子 :

1.

1.

class ABC{
----
}

ABC class not implementing or extending from technology/framework that's why this is pojo class.

ABC 类没有实现或从技术/框架扩展,这就是为什么这是 pojo 类。

2.

2.

class ABC extends HttpServlet{
---
}

ABC class extending from servlet technology api that's why this is not a pojo class.

从 servlet 技术 api 扩展的 ABC 类,这就是为什么这不是 pojo 类的原因。

3.

3.

class ABC implements java.rmi.Remote{
----
}

ABC class implements from rmi api that's why this is not a pojo class.

ABC 类从 rmi api 实现,这就是为什么这不是 pojo 类的原因。

4.

4.

class ABC implements java.io.Serializable{
---
}

this interface is part of java language not a part of technology/framework.so this is pojo class.

这个接口是 java 语言的一部分,而不是技术/框架的一部分。所以这是 pojo 类。

5.

5.

class ABC extends Thread{
--
}

here thread is also class of java language so this is also a pojo class.

这里的线程也是java语言的类,所以这也是一个pojo类。

6.

6.

class ABC extends Test{
--
}

if Test class extends or implements from technologies/framework then ABC is also not a pojo class because it inherits the properties of Test class. if Test class is not a pojo class then ABC class also not a pojo class.

如果 Test 类从技术/框架扩展或实现,那么 ABC 也不是 pojo 类,因为它继承了 Test 类的属性。如果 Test 类不是 pojo 类,那么 ABC 类也不是 pojo 类。

7.

7.

now this point is an exceptional case

现在这点是一个特例

@Entity
class ABC{
--
}

@Entityis an annotation given by hibernate api or jpa api but still we can call this class as pojo class. class with annotations given from technology/framework is called pojo class by this exceptional case.

@Entity是由 hibernate api 或 jpa api 给出的注释,但我们仍然可以将此类称为 pojo 类。在这种特殊情况下,带有从技术/框架给出的注释的类被称为 pojo 类。

回答by Trunk

You've seen the formal definitions above, for all they are worth.

你已经看到了上面的正式定义,因为它们都是有价值的。

But don't get too hung up on definitions. Let's just look more at the senseof things here.

但是不要太拘泥于定义。让我们多看看这里的事物的意义

JavaBeans are used in Enterprise Java applications, where users frequently access data and/or application code remotely, i.e. from a server (via web or private network) via a network. The data involved must therefore be streamed in serial format into or out of the users' computers - hence the need for Java EE objects to implement the interface Serializable. This much of a JavaBean's nature is no different to Java SE application objects whose data is read in from, or written out to, a file system. Using Java classes reliably over a network from a range of user machine/OS combinations also demands the adoption of conventions for their handling. Hence the requirement for implementing these classes as public, with private attributes, a no-argument constructor and standardised getters and setters.

JavaBean 用于企业 Java 应用程序,其中用户经常远程访问数据和/或应用程序代码,即通过网络从服务器(通过 Web 或专用网络)访问。因此,所涉及的数据必须以串行格式传入或传出用户的计算机——因此需要 Java EE 对象来实现 Serializable 接口。JavaBean 的大部分性质与 Java SE 应用程序对象没有什么不同,Java SE 应用程序对象的数据是从文件系统读入或写出的。通过网络从一系列用户机器/操作系统组合中可靠地使用 Java 类还需要采用约定来处理它们。因此需要将这些类实现为公共类,具有私有属性、无参数构造函数和标准化的 getter 和 setter。

Java EE applications will also use classes other than those that were implemented as JavaBeans. These could be used in processing input data or organizing output data but will not be used for objects transferred over a network. Hence the above considerations need not be applied to them bar that the be valid as Java objects. These latter classes are referred to as POJOs - Plain Old Java Objects.

Java EE 应用程序还将使用那些作为 JavaBean 实现的类以外的类。这些可用于处理输入数据或组织输出数据,但不会用于通过网络传输的对象。因此,除非它们作为 Java 对象是有效的,否则不需要将上述考虑应用于它们。后面的这些类被称为 POJO——Plain Old Java Objects。

All in all, you could see Java Beans as just Java objects adapted for use over a network.

总而言之,您可以将 Java Bean 视为仅适用于通过网络使用的 Java 对象。

There's an awful lot of hype - and no small amount of humbug - in the software world since 1995.

自 1995 年以来,软件世界有大量的炒作 - 而且还有不少骗子。

回答by ishab acharya

In summary: similarities and differences are:

总结一下:相同点和不同点是:

   java beans:                          Pojo:
-must extends serializable              -no need to extends or implement.
 or externalizable.                     
-must have public class .               - must have public class
-must have private instance variables.      -can have any access specifier variables.
-must have public setter and getter method. - may or may not have setter or getter method.
-must have no-arg constructor.           - can have constructor with agruments.

All JAVA Beans are POJO but not all POJOs are JAVA Beans.

所有 JAVA Bean 都是 POJO,但并非所有 POJO 都是 JAVA Bean。

回答by Jeril Kuruvila

Java beans are specialtype of POJOs.

Java bean 是一种特殊类型的 POJO。

Specialities listed below with reason

下面列出的专业有原因

enter image description here

在此处输入图片说明