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


当前回答

您已经看到了上面的形式定义。

但不要太纠结于定义。 让我们来看看这里的意义。

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应用程序还将使用作为javabean实现的类以外的类。它们可以用于处理输入数据或组织输出数据,但不能用于通过网络传输的对象。因此,上面的注意事项不需要应用于它们,除非它们是有效的Java对象。后面这些类被称为pojo——普通旧Java对象。

总而言之,您可以将Java bean视为适合在网络上使用的Java对象。

自1995年以来,软件世界中充斥着大量的炒作——其中不乏欺骗。

其他回答

根据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

您已经看到了上面的形式定义。

但不要太纠结于定义。 让我们来看看这里的意义。

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应用程序还将使用作为javabean实现的类以外的类。它们可以用于处理输入数据或组织输出数据,但不能用于通过网络传输的对象。因此,上面的注意事项不需要应用于它们,除非它们是有效的Java对象。后面这些类被称为pojo——普通旧Java对象。

总而言之,您可以将Java bean视为适合在网络上使用的Java对象。

自1995年以来,软件世界中充斥着大量的炒作——其中不乏欺骗。

所有的javabean都是pojo,但并非所有的pojo都是javabean。

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

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

所有Pojo(s)都是JavaBean(s),但并非相反。


什么是POJO?

POJO没有属性或方法的命名约定。对于构造、访问和修改类的状态,我们没有遵循任何真正的约定。 例子: 公共类Pojo { 字符串名字; LASTName; 字符串名称(){ 返回。firstname + " " + this.LASTName; } } 在这里,我可以用first_name或firstname或任何名词替换firstname,变量LASTName也一样。

这个词之所以被广泛接受,很可能是因为 需要一个常见的和容易理解的术语,与 复杂的对象框架


使用POJO的反射。

它可能会限制框架偏好约定而不是配置、理解如何使用类以及增强其功能的能力

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

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

[]

什么是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.

例子:

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

POJOS具有某些约定(getter/setter,公共无参数构造函数,私有变量),并且正在起作用(例如。用于按表单读取数据)是javabean。