JavaBean 和 POJO 有什么区别?

发布于 2024-08-04 02:40:45 字数 106 浏览 2 评论 0 原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

爱本泡沫多脆弱 2024-08-11 02:40:45

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.

残疾 2024-08-11 02:40:45

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

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

  • JavaBean 类必须实现 Serialized 或Externalized;
  • JavaBean 类必须有一个公共的无参数构造函数;
  • 所有 JavaBean 属性必须具有公共 setter 和 getter 方法(视情况而定);
  • 所有 JavaBean 实例变量都应该是私有的。

All JavaBeans are POJOs but not all POJOs are JavaBeans.

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

  • 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.
樱花细雨 2024-08-11 02:40:45

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

这个术语是丽贝卡·帕森斯、乔什·麦肯齐和我在创建时创造的
准备在 2000 年 9 月的一次会议上发表演讲。在演讲中,我们
我们指出了将业务逻辑编码到其中的许多好处
常规 java 对象而不是使用实体 Bean。我们想知道为什么
人们非常反对在他们的系统中使用常规对象
得出的结论是,这是因为简单的物体缺乏一个花哨的名字。所以
我们给了他们一个,而且很受欢迎。

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

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.

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

诗化ㄋ丶相逢 2024-08-11 02:40:45

Pojo - 普通的旧java对象

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

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

示例:

1.

class ABC{
----
}

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

2.

class ABC extends HttpServlet{
---
}

ABC 类从 servlet 技术 api 扩展而来,这就是为什么这不是一个 pojo 类。

3.

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

ABC 类从 rmi api 实现,这就是为什么这不是一个 pojo 类。

4.

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

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

5.

class ABC extends Thread{
--
}

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

6.

class ABC extends Test{
--
}

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

7.

现在这一点是一个例外情况

@Entity
class ABC{
--
}

@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.

class ABC{
----
}

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

2.

class ABC extends HttpServlet{
---
}

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

3.

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

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

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.

5.

class ABC extends Thread{
--
}

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

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.

7.

now this point is an exceptional case

@Entity
class ABC{
--
}

@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.

毅然前行 2024-08-11 02:40:45

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

月下客 2024-08-11 02:40:45

总结:相似点和不同点是:

   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.

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

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.

过度放纵 2024-08-11 02:40:45

Java bean 是特殊类型的 POJO。

下面列出了专业及其原因

在此处输入图像描述

Java beans are special type of POJOs.

Specialities listed below with reason

enter image description here

梦一生花开无言 2024-08-11 02:40:45

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) are JAVABEANS.

时光与爱终年不遇 2024-08-11 02:40:45

您已经看到了上面的正式定义,尽管它们很有价值。

但不要太执着于定义。
让我们更多地看看这里事物的意义

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.

掩于岁月 2024-08-11 02:40:45

所有 Pojo 都是 JavaBean,但反之则不然。


POJO是什么?

  1. POJO 没有属性或方法的命名约定。我们不遵循任何构建、访问、修改类状态的实际约定。

    示例:

     公共类 Pojo {
    
      公共字符串名字;
      公共字符串姓氏;
    
      公共字符串名称(){
         返回 this.firstname + " " + this.LASTName;
      }
     }
    

    在这里,我可以将 firstname 替换为 first_nameFirstname 或任何 名词 与变量 LASTName 相同。

该术语很可能获得广泛接受,因为
需要一个通用且易于理解的术语来对比
复杂的对象框架。[2]


使用 POJO 进行反射。

它可能会限制框架支持约定优于配置、了解如何使用类以及增强其功能的能力。[1]

  List<String> propertyNames =
                Arrays.stream(PropertyUtils.getPropertyDescriptors(Pojo.class))
                        .map(PropertyDescriptor::getDisplayName)
                        .collect(Collectors.toList());
        System.out.println(propertyNames);

如果我们使用第三方库 PropertyUtils 进行反射,我们可能会遇到问题,因为这将导致

[]

什么是 Java Beans?

JavaBean 仍然是 POJO,但引入了一组严格的规则
我们如何实现它:

  • 访问级别 - 我们的属性是私有的,并且我们公开 getter 和 setter。
  • 方法名称 - 我们的 getter 和 setter 遵循 getX 和
    setX 约定(对于布尔值,isX 可用于
    吸气剂)
  • 默认构造函数 - 无参构造函数必须是
    存在,因此可以在不提供参数的情况下创建实例,例如
    反序列化期间的示例
  • 可序列化 - 实现可序列化接口允许我们存储状态。

示例:

@Getter
@Setter
 class Pojo implements Serializable {
    public String firstName;
    public String lastName;
}

使用 Java Bean 进行反射。

If we again use third party Libraries such as `PropertyUtils` for reflection the result will be different

[firstName,lastName]

All Pojo(s) are JavaBean(s), but not the opposite.


What is POJO?

  1. 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:

     public class Pojo {
    
      public String firstname;
      public String LASTName;
    
      public String name() {
         return this.firstname + " " + this.LASTName;
      }
     }
    

    here, I could have replaced firstname by first_name or Firstname or by any noun and the same with the variable LASTName.

The term has most likely gained widespread acceptance because of the
need for a common and easily understood term that contrasts with
complicated object frameworks.[2]


Reflection using POJO.

it may limit a framework's ability to favor convention over configuration, understand how to use the class, and augment its functionality.[1]

  List<String> propertyNames =
                Arrays.stream(PropertyUtils.getPropertyDescriptors(Pojo.class))
                        .map(PropertyDescriptor::getDisplayName)
                        .collect(Collectors.toList());
        System.out.println(propertyNames);

If we use Third Party Libraries PropertyUtils for reflection we may face issues, as this will result in

[]

What is Java Beans?

A JavaBean is still a POJO but introduces a strict set of rules around
how we implement it:

  • Access levels – our properties are private and we expose getters and setters.
  • Method names – our getters and setters follow the getX and
    setX convention (in the case of a boolean, isX can be used for a
    getter)
  • Default Constructor – a no-argument constructor must be
    present so an instance can be created without providing arguments, for
    example during deserialization
  • Serializable – implementing the Serializable interface allows us to store the state.

Example:

@Getter
@Setter
 class Pojo implements Serializable {
    public String firstName;
    public String lastName;
}

Reflection using Java Bean.

If we again use third party Libraries such as `PropertyUtils` for reflection the result will be different

[firstName,lastName]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文