我不确定有什么区别。我正在使用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年以来,软件世界中充斥着大量的炒作——其中不乏欺骗。
其他回答
Java bean是特殊类型的pojo。
以下列出的特色是有道理的
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。
所有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]
POJO:如果这个类可以在底层JDK中执行,而不需要任何外部第三方库的支持,那么它就被称为POJO
JavaBean:如果类只包含带有访问器(setter和getter)的属性,则称为JavaBean。JavaBean通常不包含任何业务逻辑,而是用于保存一些数据。
所有的爪哇人都是POJO,但并非所有POJO都是爪哇人