JavaBean 和 POJO 有什么区别?
我不确定其中的区别。我使用 Hibernate,在一些书中,他们使用 JavaBean 和 POJO 作为可互换的术语。我想知道是否存在差异,不仅在 Hibernate 上下文中,而且作为一般概念。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我不确定其中的区别。我使用 Hibernate,在一些书中,他们使用 JavaBean 和 POJO 作为可互换的术语。我想知道是否存在差异,不仅在 Hibernate 上下文中,而且作为一般概念。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
JavaBean 遵循某些约定。 Getter/setter 命名、具有公共默认构造函数、可序列化等。请参阅 JavaBeans约定了解更多详细信息。
POJO(普通旧 Java 对象)没有严格定义。它是一个 Java 对象,不需要实现特定的接口或从特定的基类派生,或使用特定的注释来与给定的框架兼容,并且可以是任意的(通常相对简单) Java 对象。
A JavaBean follows certain conventions. Getter/setter naming, having a public default constructor, being serialisable etc. See JavaBeans Conventions for more details.
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.
所有 JavaBean 都是 POJO,但并非所有 POJO 都是 JavaBean。
JavaBean 是满足某些编程约定的 Java 对象:
All JavaBeans are POJOs but not all POJOs are JavaBeans.
A JavaBean is a Java object that satisfies certain programming conventions:
根据 Martin Fowler 的说法,POJO 是一个封装业务逻辑的对象,而 Bean(除了其他答案中已经说明的定义)只不过是一个用于保存数据的容器,对象上可用的操作只是设置和获取数据。
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.
Pojo - 普通的旧java对象
pojo类是一个没有任何特殊性的普通类,与技术/框架完全松散耦合的类。该类不从技术/框架实现,也不从技术/框架 api 扩展该类称为 pojo 类。
pojo类可以实现接口并扩展类,但超类或接口不应该是技术/框架。
示例:
1.
ABC 类未从技术/框架实现或扩展,这就是为什么这是 pojo 类。
2.
ABC 类从 servlet 技术 api 扩展而来,这就是为什么这不是一个 pojo 类。
3.
ABC 类从 rmi api 实现,这就是为什么这不是一个 pojo 类。
4.
这个接口是java语言的一部分,而不是技术/框架的一部分。所以这是pojo类。
5.
这里的thread也是java语言的类,所以这也是一个pojo类。
6.
如果Test类从技术/框架扩展或实现,那么ABC也不是pojo类,因为它继承了Test类的属性。
如果 Test 类不是 pojo 类,那么 ABC 类也不是 pojo 类。
7.
现在这一点是一个例外情况
@Entity
是 hibernate api 或 jpa api 给出的注释,但我们仍然可以将此类称为 pojo 类。在这种特殊情况下,具有技术/框架给出的注释的类被称为 pojo 类。
Pojo - Plain old java object
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 class can implements interfaces and extend classes but the super class or interface should not be an technology/framework.
Examples :
1.
ABC class not implementing or extending from technology/framework that's why this is pojo class.
2.
ABC class extending from servlet technology api that's why this is not a pojo class.
3.
ABC class implements from rmi api that's why this is not a pojo class.
4.
this interface is part of java language not a part of technology/framework.so this is pojo class.
5.
here thread is also class of java language so this is also a pojo class.
6.
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.
7.
now this point is an exceptional case
@Entity
is 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.
POJO:如果类可以使用底层 JDK 执行,无需任何其他外部第三方库支持,则称为 POJO
JavaBean:如果类仅包含带有访问器(setter 和 getter)的属性,则这些属性称为 javabean。Java bean 通常不会包含任何属性业务逻辑而不是用于在其中保存一些数据的逻辑。
所有 Javabean 都是 POJO,但所有 POJO 都不是 Javabean
POJO: If the class can be executed with underlying JDK,without any other external third party libraries support then its called 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.
All Javabeans are POJOs but all POJO are not Javabeans
总结:相似点和不同点是:
所有 JAVA Bean 都是 POJO,但并非所有 POJO 都是 JAVA Bean。
In summary: similarities and differences are:
All JAVA Beans are POJO but not all POJOs are JAVA Beans.
下面列出了专业及其原因
Specialities listed below with reason
POJOS
具有某些约定(getter/setter、公共无参数构造函数、私有变量)并且正在运行(例如,用于按表单读取数据)是JAVABEANS
。POJOS
with certain conventions (getter/setter,public no-arg constructor ,private variables) and are in action(ex. being used for reading data by form) areJAVABEANS
.您已经看到了上面的正式定义,尽管它们很有价值。
但不要太执着于定义。
让我们更多地看看这里事物的意义。
JavaBean 用于企业 Java 应用程序,其中用户经常远程访问数据和/或应用程序代码,即通过网络从服务器(通过 Web 或专用网络)访问数据和/或应用程序代码。因此,所涉及的数据必须以串行格式流入或流出用户的计算机 - 因此需要 Java EE 对象实现 Serialized 接口。 JavaBean 的本质与 Java SE 应用程序对象没有什么不同,后者的数据是从文件系统读入或写出的。
通过网络从一系列用户机器/操作系统组合可靠地使用 Java 类还需要采用它们的处理约定。因此,需要将这些类实现为公共类,并具有私有属性、无参构造函数以及标准化的 getter 和 setter。
Java EE 应用程序还将使用除作为 JavaBean 实现的类之外的类。它们可用于处理输入数据或组织输出数据,但不会用于通过网络传输的对象。因此,除非它们作为 Java 对象有效,否则不需要对它们应用上述考虑因素。后面的这些类被称为 POJO - 普通旧 Java 对象。
总而言之,您可以将 Java Bean 视为适合在网络上使用的 Java 对象。
自 1995 年以来,软件世界中充斥着大量的炒作,同时也存在不少的骗局。
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 sense of 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.
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.
All in all, you could see Java Beans as just Java objects adapted for use over a network.
There's an awful lot of hype - and no small amount of humbug - in the software world since 1995.
所有 Pojo 都是 JavaBean,但反之则不然。
POJO是什么?
POJO 没有属性或方法的命名约定。我们不遵循任何构建、访问、修改类状态的实际约定。
示例:
在这里,我可以将
firstname
替换为first_name
或Firstname
或任何 名词 与变量LASTName
相同。使用 POJO 进行反射。
如果我们使用第三方库
PropertyUtils
进行反射,我们可能会遇到问题,因为这将导致什么是 Java Beans?
示例:
使用 Java Bean 进行反射。
If we again use third party Libraries such as `PropertyUtils` for reflection the result will be different
All Pojo(s) are JavaBean(s), but not the opposite.
What is POJO?
A POJO has no naming convention for properties or methods. We don't follow any real convention for constructing, accessing, modifying the class's state.
Example:
here, I could have replaced
firstname
byfirst_name
orFirstname
or by any noun and the same with the variableLASTName
.Reflection using POJO.
If we use Third Party Libraries
PropertyUtils
for reflection we may face issues, as this will result inWhat is Java Beans?
Example:
Reflection using Java Bean.
If we again use third party Libraries such as `PropertyUtils` for reflection the result will be different