什么是POJO弧岳,JavaBean?
總結(jié):
POJO:
一個簡單的Java類业踏,這個類沒有實現(xiàn)/繼承任何特殊的java接口或者類,不遵循任何主要java模型涧卵,約定或者框架的java對象勤家。在理想情況下,POJO不應(yīng)該有注解柳恐。
JavaBean:
- JavaBean是可序列化的伐脖,實現(xiàn)了serializable接口
- 具有一個無參構(gòu)造器
- 有按照命名規(guī)范的set和gett,is(可以用于訪問布爾類型的屬性)方法
pojo
點擊鏈接:java對象 POJO和JavaBean的區(qū)別
點擊鏈接:Difference between DTO, VO, POJO, JavaBeans?
點擊鏈接:維基百科:Plain old Java object
POJO的創(chuàng)始人(martinfowler)博客:
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.
...在談話中我們指出乐设,編寫業(yè)務(wù)邏輯的時候讼庇,使用常規(guī)的java對象要比實體bean要好的多。我們懷疑為什么一些人極力反對在他們的代碼中使用常規(guī)對象近尚,還辯解稱因為這些常規(guī)對象沒有一個花哨的名字蠕啄,所以我們給他們起了一個非常好聽的名字。(Plain Old Java Object)
維基百科原文————(以下代碼和示例均來源于此)
The term "POJO" initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks; nowadays "POJO" may be used as an acronym for "Plain Old JavaScript Object" as well, in which case the term denotes a JavaScript object of similar pedigree.[2]
術(shù)語POJO起初表示為不遵任何主要的java模型戈锻,約定或者框架的java對象歼跟,現(xiàn)在,pojo也可以用作'Plain Old JavaScript Object'的縮寫格遭,這樣的話和javascript對象有著相似的淵源哈街。
理想狀態(tài)下,pojo應(yīng)該是一個不受Java語言規(guī)范限制的java對象拒迅。換句話說骚秦,pojo<strong>不應(yīng)該</strong>:
1.繼承預(yù)先設(shè)定的類她倘,如:
public class Foo extends javax.servlet.http.HttpServlet {
// ...
}
2.實現(xiàn)預(yù)先設(shè)定的接口,如:
public class Bar implements javax.ejb.EntityBean {
// ...
}
3.包含預(yù)先指定的注解作箍,如:
@javax.persistence.Entity
public class Baz {
// ...
}
However, due to technical difficulties and other reasons, many software products or frameworks described as POJO-compliant actually still require the use of prespecified annotations for features such as persistence to work properly. The idea is that if the object (actually class) was a POJO before any annotations were added, and would return to POJO status if the annotations are removed then it can still be considered a POJO. Then the basic object remains a POJO in that it has no special characteristics (such as an implemented interface) that makes it a "Specialized Java Object" (SJO or (sic) SoJO).
然而硬梁,由于技術(shù)和其他原因,很多被稱之為POJO標準的軟件產(chǎn)品或框架仍然需要使用特定的注解來保證持久化等功能蒙揣。這個想法是靶溜,如果對象(類)在任何注解添加之前是一個pojo的話,并且注解移除之后仍然是pojo懒震。所以最基礎(chǔ)的pojo解釋是沒有特別的特征(尤其是實現(xiàn)接口之類的)罩息,使其稱之為“專用java對象”
JavaBean
點此鏈接:維基百科中JavaBeans的概念
A JavaBean is a POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention. Because of this convention, simple declarative references can be made to the properties of arbitrary JavaBeans. Code using such a declarative reference does not have to know anything about the type of the bean, and the bean can be used with many frameworks without these frameworks having to know the exact type of the bean. The JavaBeans specification, if fully implemented, slightly breaks the POJO model as the class must implement the Serializable interface to be a true JavaBean. Many POJO classes still called JavaBeans do not meet this requirement. Since Serializable is a marker (method-less) interface, this is not much of a burden.
JavaBean是一個可序列化的POJO,具有一個無參構(gòu)造器个扰,并且允許使用遵循簡單命名約定的getter和setter方法來訪問屬性瓷炮。由于這個慣例,可以對任意JavaBean屬性進行簡單的聲明引用递宅。使用這種聲明引用的代碼不需要知道bean的具體類型娘香。并且,這個bean還可以被很多框架使用办龄,這些java框架也不需要知道bean的類型烘绽。由于java.io.Serializable是一個標記接口(無方法),所以這并不是一個多大的負擔(dān)俐填。如果JavaBean完全實現(xiàn)的話安接,稍微打破了一些POJO模型。很多被稱之為JavaBean的POJO類并不符合這個要求英融,因為JavaBean必須實現(xiàn)Serializable接口才能成為真正的JavaBean盏檐。
JavaBean的優(yōu)點:
The properties, events, and methods of a bean can be exposed to another application.
A bean may register to receive events from other objects and can generate events that are sent to those other objects.
Auxiliary software can be provided to help configure a bean.
The configuration settings of a bean can be saved to persistent storage and restored.
- bean中的屬性,事件和方法可以暴露給另一個應(yīng)用程序
- 一個bean可以注冊來自于其他對象的事件驶悟,也可以產(chǎn)生事件并發(fā)送給其他對象
- 輔助代碼可以提供javabean的配置
- 一個bean的配置設(shè)置可以永遠被存儲和恢復(fù)
一些疑問胡野?
- 什么是事件?有什么作用痕鳍?bean如何注冊來自于其他對象的事件硫豆?
- 配置設(shè)置如何被存儲和恢復(fù)?
- serializable接口是做什么的额获?