我不确定有什么区别。我正在使用Hibernate,在一些书中,他们使用JavaBean和POJO作为可互换的术语。我想知道是否有区别,不仅仅是在Hibernate上下文中,而是在一般概念上。
当前回答
Java bean是特殊类型的pojo。
以下列出的特色是有道理的
其他回答
根据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
POJOS具有某些约定(getter/setter,公共无参数构造函数,私有变量),并且正在起作用(例如。用于按表单读取数据)是javabean。
普通的旧java对象
Pojo类是一个没有任何特殊特性的普通类,从技术/框架上完全松散耦合。这个类不是从技术/框架实现的,也不是从技术/框架API扩展的,这个类叫做pojo类。
Pojo类可以实现接口和扩展类,但超类或接口不应该是一种技术/框架。
例子:
1.
class ABC{
----
}
ABC类没有实现或从技术/框架扩展,这就是为什么这是pojo类。
2.
class ABC extends HttpServlet{
---
}
从servlet技术api扩展的ABC类,这就是为什么这不是一个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{
--
}
这里线程也是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类。 在这种例外情况下,带有来自technology/framework的注释的类称为pojo类。
您已经看到了上面的形式定义。
但不要太纠结于定义。 让我们来看看这里的意义。
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年以来,软件世界中充斥着大量的炒作——其中不乏欺骗。
POJO:如果这个类可以在底层JDK中执行,而不需要任何外部第三方库的支持,那么它就被称为POJO
JavaBean:如果类只包含带有访问器(setter和getter)的属性,则称为JavaBean。JavaBean通常不包含任何业务逻辑,而是用于保存一些数据。
所有的爪哇人都是POJO,但并非所有POJO都是爪哇人
推荐文章
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列
- 如何运行一个类从Jar不是主类在其清单文件
- 使arrayList.toArray()返回更具体的类型
- 在Eclipse中粘贴多行Java字符串