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


当前回答

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

其他回答

综上所述:相同点和不同点是:

   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。

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

所有的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]

Java bean是特殊类型的pojo。

以下列出的特色是有道理的