我不确定有什么区别。我正在使用Hibernate,在一些书中,他们使用JavaBean和POJO作为可互换的术语。我想知道是否有区别,不仅仅是在Hibernate上下文中,而是在一般概念上。
当前回答
综上所述:相同点和不同点是:
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
POJOS具有某些约定(getter/setter,公共无参数构造函数,私有变量),并且正在起作用(例如。用于按表单读取数据)是javabean。
您已经看到了上面的形式定义。
但不要太纠结于定义。 让我们来看看这里的意义。
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。
以下列出的特色是有道理的
所有的javabean都是pojo,但并非所有的pojo都是javabean。
JavaBean是满足某些编程约定的Java对象:
JavaBean类必须实现Serializable或Externalizable; JavaBean类必须有一个公共的无参数构造函数; 所有JavaBean属性必须有公共setter和getter方法(视情况而定); 所有JavaBean实例变量都应该是私有的。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 数据挖掘中分类和聚类的区别?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder