java Model、javabean 和 POJO 的区别

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

Difference among Model, javabean and POJO

javamodeljavabeanspojo

提问by Varun

I started learning MVC with spring. I have heard lot of time Bean, that contains setter and getter. Modelis basically what data flows around, and Pojowhich is same as Bean. But I am really confused in all this term and all this look same to me can you please explain the exact difference among all of them.

我从 spring 开始学习 MVC。我听说过很多 time Bean,其中包含 setter 和 getter。Model基本上是什么数据周围流动,而Pojo这是一样的Bean。但是我对这个术语真的很困惑,所有这些在我看来都一样,你能解释一下它们之间的确切区别吗?

JAVABEAN

爪哇

POJO

POJO

MODEL

模型

回答by Fourat

If you're using the MVC architecture then the Model represents your domain: means your entities and it's not a java related term.
Your Models are represented in Java as Java Beans (best practice in Java EE).
A Java Bean is a normal Java class which implements the Serializable interface and have a parameterless constructor and have getters and setters for each field.

如果您使用的是 MVC 架构,那么模型代表您的领域:意味着您的实体,它不是与 Java 相关的术语。
您的模型在 Java 中表示为 Java Bean(Java EE 中的最佳实践)。
Java Bean 是一个普通的 Java 类,它实现了 Serializable 接口并具有一个无参数的构造函数,并且每个字段都有 getter 和 setter。

However POJO is just a denomination for objects not bound by any restriction other than those forced by the Java Language Specification (Wikipeadia). This is just for conventions sake and it's not strictly related to the MVC architecture.
Note that Java beans are POJOs implementing the Serializable interface.

然而,POJO 只是对象的一种名称,除了 Java 语言规范 ( Wikipeadia)强制的那些限制之外,不受任何限制的约束。这只是为了约定俗成,与 MVC 架构没有严格的关系。
请注意,Java bean 是实现 Serializable 接口的 POJO。

回答by Pratik

Only difference is bean can be serialized.

唯一的区别是 bean 可以序列化。

From Java docs - http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

来自 Java 文档 - http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

类的可序列化由实现 java.io.Serializable 接口的类启用。未实现此接口的类将不会对其任何状态进行序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。

While model is different thing which is dealing with your business logic.

而模型是处理您的业务逻辑的不同事物。

you can refere below link

你可以参考下面的链接

Programming difference between POJO and Bean

POJO和Bean的编程区别

回答by Eugene

As a supplement, it's necessary to describe the intention of each item.

作为补充,有必要描述每个项目的意图。

As defined from wiki,

正如维基定义的那样,

The term "POJO" initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks

Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification

术语“POJO”最初表示不遵循任何主要 Java 对象模型、约定或框架的 Java 对象

理想情况下,POJO 是一个 Java 对象,除了 Java 语言规范强制的限制之外,不受任何限制的约束

Generally, a POJO doesn't dependent on any library, interface or annotation. Therefore, a POJO is more likely to be reused by different system.

通常,POJO 不依赖于任何库、接口或注释。因此,一个 POJO 更有可能被不同的系统重用。

Ok, so what is Java Bean and why we create this item?
The description from this linkclarified it clear enough I think.

好的,那么什么是 Java Bean?我们为什么要创建这个项目?我认为这个链接
的描述已经足够清楚了。

JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.

JavaBean 是将许多对象封装到单个对象(bean)中的类。它们是可序列化的,具有零参数构造函数,并允许使用 getter 和 setter 方法访问属性。

Why we want Jave beans to behave like this?

为什么我们希望 Jave beans 有这样的行为?

  • The class must have a public default constructor (with no arguments).
  • 该类必须有一个公共默认构造函数(没有参数)。

This allows easy instantiation within editing and activation frameworks.

这允许在编辑和激活框架内轻松实例化。

  • The class properties must be accessible using get, set, is (can be used for boolean properties instead of get), to and other methods (so-called accessor methods and mutator methods) according to a standard naming convention.
  • 根据标准命名约定,必须可以使用 get、set、is(可用于布尔属性而不是 get)、to 和其他方法(所谓的访问器方法和修改器方法)访问类属性。

This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more than one argument.

这允许在框架内轻松自动检查和更新 bean 状态,其中许多包括用于各种类型属性的自定义编辑器。Setter 可以有一个或多个参数。

  • The class should be serializable.
  • 该类应该是可序列化的。

This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.

这允许应用程序和框架以独立于 VM 和平台的方式可靠地保存、存储和恢复 bean 的状态。

Generally, the model isn't compared with POJO or JaveBean cause it's quite a different item. Like what has mentioned by other answer, the model is generally a concept from MVC.

通常,模型不会与 POJO 或 JaveBean 进行比较,因为它是完全不同的项目。就像其他答案提到的那样,该模型通常是来自MVC的概念。

The model is the central component of the pattern. It is the application's dynamic data structure, independent of the user interface.[6] It directly manages the data, logic and rules of the application.

模型是模式的核心组件。它是应用程序的动态数据结构,独立于用户界面。[6] 它直接管理应用程序的数据、逻辑和规则。

As you can see, POJO or JavaBean can be at model layer in MVC pattern but model layer but there are a lot more stuff in model layer, for example, the logic and rules of the application.

如您所见,POJO 或 JavaBean 可以在 MVC 模式中位于模型层,但在模型层,但模型层中有更多的东西,例如应用程序的逻辑和规则。