POJO何解?

因项目需要被逼捡起JAVA并深入学习一把。过程中很多地方看到POJO这个词语,其全称是Plain Old Java Object。当我询问一个Java做得不错的朋友时,告诉我POJO是JavaBean的一种,它除了private属性以及对这个属性方法的public的get和set方法之外没有其他任何东西。它只能装载数据,作为数据存储的载体,而不具有业务逻辑处理的能力。

感觉有些不对劲,Google了一把:POJO的叫法是Martin Fowler、Rebecca Parsons和Josh MacKenzie在2000年的一次演讲的时候提出来的。初始出处是http://martinfowler.com/bliki/POJO.html: > 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.

其意思是说要给具有业务逻辑处理的规则的Java对象(regular java objects)但又不是Entity Beans(EJB的一种)起一个名字。他们就叫这样的Java对象为POJO,在http://www.webopedia.com/TERM/P/POJO.htm是这样定义POJO的:> POJO, or Plain Old Java Object, is a normal Java object class (that is, not a JavaBean, EntityBean etc.) and does not serve any other special role nor does it implement any special interfaces of any of the Java frameworks. This term was coined by Martin Fowler, Rebbecca Parsons and Josh MacKenzie who believed that by creating the acronym POJO, such objects would have a “fancy name”, thereby convincing people that they were worthy of use.

基本意思是说POJO一个正常的Java对象(不是JavaBean,EntityBean等),也不担当任何的特殊的角色,也不实现任何Java框架中特定的接口。

这个解释应该是POJO的准确解释,POJO不是Java Bean,更不是EJB,不应该依赖于框架(继承或实现某些框架类或接口),POJO不具有任何特殊角色。POJO是可以包含业务逻辑处理和持久化逻辑的,也可以包含与JavaBean类似的属性和对属性相应访问setter、getter的。