DTO、VO、POJO、JavaBean 之间的区别?
Have seen some similar questions:
- What is the difference between a JavaBean and a POJO?
- What is the Difference Between POJO (Plain Old Java Object) and DTO (Data Transfer Object)?
Can you also please tell me the contexts in which they are used? Or the purpose of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
JavaBean
JavaBean 是一个遵循 JavaBeans 约定 的类正如 Sun 所定义的。维基百科对 JavaBean 进行了很好的总结:
POJO
普通旧 Java 对象或 POJO 是一个最初引入的术语,用于指定简单的轻量级 Java 对象,不实现任何
javax.ejb
接口,与重量级 EJB 2.x(尤其是实体 Bean、无状态在我看来,会话 Bean 并没有那么糟糕)。今天,这个术语用于任何没有额外内容的简单对象。同样,维基百科在定义 POJO 方面做得很好:值对象
值对象或 VO 是一个诸如 java.lang.Integer 之类的对象,它保存值(因此是值对象)。对于更正式的定义,我经常参考 Martin Fowler 对 Value Object 的描述:
数据传输对象
数据传输对象或 DTO 是 EJB 引入的一种(反)模式。这个想法不是在 EJB 上执行许多远程调用,而是将数据封装在可以通过网络传输的值对象中:数据传输对象。维基百科对数据传输对象有一个不错的定义:
因此,对于许多人来说,DTO 和 VO 是同一件事(但正如我们所见,Fowler 使用 VO 来表示其他含义)。大多数时候,它们遵循 JavaBean 约定,因此也是 JavaBean。而且都是 POJO。
JavaBeans
A JavaBean is a class that follows the JavaBeans conventions as defined by Sun. Wikipedia has a pretty good summary of what JavaBeans are:
POJO
A Plain Old Java Object or POJO is a term initially introduced to designate a simple lightweight Java object, not implementing any
javax.ejb
interface, as opposed to heavyweight EJB 2.x (especially Entity Beans, Stateless Session Beans are not that bad IMO). Today, the term is used for any simple object with no extra stuff. Again, Wikipedia does a good job at defining POJO:Value Object
A Value Object or VO is an object such as
java.lang.Integer
that hold values (hence value objects). For a more formal definition, I often refer to Martin Fowler's description of Value Object:Data Transfer Object
Data Transfer Object or DTO is a (anti) pattern introduced with EJB. Instead of performing many remote calls on EJBs, the idea was to encapsulate data in a value object that could be transfered over the network: a Data Transfer Object. Wikipedia has a decent definition of Data Transfer Object:
So, for many people, DTOs and VOs are the same thing (but Fowler uses VOs to mean something else as we saw). Most of time, they follow the JavaBeans conventions and are thus JavaBeans too. And all are POJOs.
DTO 与 VO
DTO vs VO
???? DTO - Data Transfer Object is just a data container which is used to transport data between layers and tiers.
DTO was mainly used to get data transported across the network efficiently, it may be even from JVM to another JVM.
DTOs are often
java.io.Serializable
- in order to transfer data across JVM.???? VO - Value Object [1][2] represents a fixed set of data and is similar to a Java enum. A Value Object's identity is based on their state rather than on their object identity and is immutable. A real world example would be
Color.RED
,Color.BLUE
,SEX.FEMALE
etc.POJO vs JavaBeans
[1]
The "Java-Beanness" of a POJO is that its private attributes are all accessed via public getters and setters that conform to the JavaBeans conventions. e.g.
[2]
JavaBeans must implement
Serializable
and have a no-argument constructor, whereas in POJO does not have these restrictions.基本上,
DTO:“数据传输对象”可以在软件架构的不同层之间传输。
VO:“值对象”持有整数、货币等对象。
POJO:普通旧 Java 对象,不是特殊对象。
Java Bean:需要可序列化的 Java 类、无参数构造函数以及每个字段的 getter 和 setter
Basically,
DTO: "Data transfer objects " can travel between seperate layers in software architecture.
VO: "Value objects " hold a object such as Integer,Money etc.
POJO: Plain Old Java Object which is not a special object.
Java Beans: requires a
Java Class
to be serializable, have ano-arg
constructor and a getter and setter for each fieldJava Bean 与 EJB 不同。
Java 1.0 中的 JavaBeans 规范 是 Sun 试图允许的在类似于 VB 的 IDE 中操作 Java 对象。对于符合“Java Beans”资格的对象制定了规则:
EJB 后来出现。它们结合了分布式组件和事务模型,在管理线程、池、生命周期并提供服务的容器中运行。它们与 Java Bean 相去甚远。
DTO 在 Java 环境中出现,因为人们发现 EJB 1.0 规范与数据库过于“啰嗦”。人们不会对每个数据元素进行往返,而是将它们批量打包成 Java Bean 并进行传送。
POJO 是针对 EJB 的反应。
Java Beans are not the same thing as EJBs.
The JavaBeans specification in Java 1.0 was Sun's attempt to allow Java objects to be manipulated in an IDE that looked like VB. There were rules laid down for objects that qualified as "Java Beans":
EJBs came later. They combine distributed components and a transactional model, running in a container that manages threads, pooling, life cycle, and provides services. They are a far cry from Java Beans.
DTOs came about in the Java context because people found out that the EJB 1.0 spec was too "chatty" with the database. Rather than make a roundtrip for every data element, people would package them into Java Beans in bulk and ship them around.
POJOs were a reaction against EJBs.
POJO:
它是一个java文件(类),它不扩展或实现任何其他java文件(类)。
豆:
它是一个java文件(类),其中所有变量都是私有的,方法是公共的,并且使用适当的getter和setter来访问变量。
普通班:
它是一个java文件(类),可能由公共/私有/默认/受保护变量组成,并且可能会也可能不会扩展或实现另一个java文件(类)。
POJO :
It is a java file(class) which doesn't extend or implement any other java file(class).
Bean:
It is a java file(class) in which all variables are private, methods are public and appropriate getters and setters are used for accessing variables.
Normal class:
It is a java file(class) which may consist of public/private/default/protected variables and which may or may not extend or implement another java file(class).
值对象模式与数据传输模式之间的差异
difference-between-value-object-pattern-and-data-transfer-pattern
首先谈谈
普通类-这意味着任何在java中通常定义的类,这意味着您创建不同类型的方法属性等
Bean - Bean 没什么,它只是该特定类的一个对象,使用此 Bean,您可以像对象一样访问您的 java 类。.
然后讨论最后一个 POJO
< Strong>POJO - POJO 是没有任何服务的类,它只有一个默认构造函数和私有属性以及用于设置相应的 setter 和 getter 方法的值的属性。
它是纯 Java 对象的缩写形式。
First Talk About
Normal Class - that's mean any class define that's a normally in java it's means you create different type of method properties etc.
Bean - Bean is nothing it's only a object of that particular class using this bean you can access your java class same as object..
and after that talk about last one POJO
POJO - POJO is that class which have no any services it's have only a default constructor and private property and those property for setting a value corresponding setter and getter methods.
It's short form of Plain Java Object.
POJO(普通Java对象)
JavaBean 只是一个 POJO,应用了一些额外的规则。规则:
PS 资源: baeldung & Udemy 以及此线程中的其他响应< /em>
POJO (plain old Java object)
A JavaBean is just a POJO, with some extra rules applied to it. Rules:
P.S. resources: baeldung & Udemy and also other responses in this thread
首先,应该明确使用哪种类型以及何时使用。
DTO(数据传输对象)
数据传输对象(简称 DTO)是用于跨进程移动数据的简单对象。其主要应用涉及通过网络或应用程序的多层之间的数据传输。 DTO 通常具有私有字段以及 getter 和 setter 方法来访问它们所保存的数据。值得注意的是,DTO 充当应用程序的显示层和业务逻辑层之间信息交换的手段,但缺乏业务逻辑或验证过程。
VO(值对象)
值对象(VO)用于数据传输,与 DTO 类似。然而,与保存在数据库中或在进程之间传输的数据相反,VO 明确用于表示用户可访问的数据。在实际使用中,VO位于应用程序的显示层中。它们还可能结合进一步的技术,旨在通过数据格式化或操作来改进用户界面。
POJO(普通旧式 Java 对象):
普通旧式 Java 对象(POJO)是基本的 Java 实体,没有任何独特的方法或特征。它们提供用于修改和检索数据的访问器方法和私有字段。 POJO 充当基本数据承载者,并且可以充当 DTO 或 VO。
JavaBeans:
JavaBeans 是将许多对象封装到单个对象(bean)中的类。相反,JavaBean 遵循特定的命名和访问标准。这些对象提供公共 getter 和 setter 方法来访问数据属性以及私有字段。通过 JavaBeans 确保一致且可移植的数据表示。通过各种工具和框架,它们还使序列化、反序列化和可访问性变得更容易。
有关更多详细信息,以下内容提供了清晰的概念。
First of all, it should be clear which type to use and when.
DTO (Data Transfer Object)
Data transfer objects, or DTOs for short, are simple objects used to move data across processes. Its main applications involve data transmission through networks or between applications' many layers. DTOs typically have private fields as well as getter and setter methods for gaining access to the data they hold. Notably, DTOs act as a means of information interchange between the display layer and the business logic layer of an application but lack business logic or validation procedures.
VO (Value Object)
Value Objects (VOs), which are used for data transport, are similar to DTOs. However, as opposed to data that is kept in databases or transmitted between processes, VOs are expressly used to represent data that is accessible to the user. In actual use, VOs are positioned in an application's display layer. They might also incorporate further techniques designed to improve the user interface through data formatting or manipulation.
POJO (Plain Old Java Object) :
Plain Old Java Objects, or POJOs, are basic Java entities without any unique methods or characteristics. They provide accessor methods and private fields for modifying and retrieving data. POJOs serve as basic data bearers and could act as DTOs or VOs.
JavaBeans:
JavaBeans are classes that encapsulate many objects into a single object (the bean). Contrarily, JavaBeans follow particular naming and access standards. These objects offer public getter and setter methods for gaining access to data attributes as well as private fields. Consistent and portable data representation is ensured via JavaBeans. Through a variety of tools and frameworks, they also make serialization, deserialization, and accessibility easier.
For more detail information, the following contents provide clear concept.