POJO(Plain Old Java Object)这种叫法是Martin Fowler、Rebecca Parsons和Josh MacKenzie在2000年的一次演讲的时候提出来的。
我在做J2EE培训中发现我的很多学生问我什么是POJO,后来我在写书(《Spring2初学者实践教材》和《Spring3初学者实践教材》)的时候发现POJO这个概念无法回避。现在网上对于POJO的解释很多,但是很多都是有错误的或者不够准确。对此我一开始也是存在误区的,我原来是这样理解的:
POJO是这样的一种“纯粹的”JavaBean,在它里面除了JavaBean规范的方法和属性没有别的东西,即private属性以及对这个属性方法的public的get和set方法。我们会发现这样的JavaBean很“单纯”,它只能装载数据,作为数据存储的载体,而不具有业务逻辑处理的能力。
所以下面的代码被认为是POJO了。
package com.tongking.spring;
public class DbHello implements Hello {
private DictionaryDAO dao;
public void setDao(DictionaryDAO dao) {
this.dao = dao;
}
}
其实,这样的认为是错误的,我仔细阅读了《POJOs in Action》这本书的有关部分和POJO的最原始的出处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)起了一个名字——POJO,这些Java对象不是EntityBeans(EJB的一种)。
我又在http://www.webopedia.com/TERM/P/POJO.htm查到解释如下:
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应该不是我们开始认为的JavaBean,当然更不是EJB,它不应该依赖于框架即继承或实现某些框架类或接口。例如:Struts1中的Action和ActionForm当然不属于POJO了,而在Struts2中的Action由于可以不继承任何的接口,所以在这种情况下Action是POJO,但是Struts2中的Action也可以继承ActionSupport类就不再属于POJO了。POJO里面是可以包含业务逻辑处理和持久化逻辑,也可以包含类似与JavaBean属性和对属性访问的set和get方法的。
最后,我们总结一下给一个定义把,POJO是一个简单的、正规Java对象,它包含业务逻辑处理或持久化逻辑等,但不是JavaBean、EntityBean等,不具有任何特殊角色和不继承或不实现任何其它Java框架的类或接口。
文章出处:http://www.diybl.com/course/3_program/java/javashl/200845/108451.html
分享到:
相关推荐
面向初学者,深入浅出,通俗易懂的算法介绍。Once Upon an Algorithm: How Stories Explain Computing
In this unique and insightful book, leading computer scientists offer case studies that reveal how they found unusual, carefully designed solutions to high-profile projects. You will be able to look ...
Once Upon an Algorithm How Stories Explain Computing 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络...
This document presumptuously purports to explain how to do research . We give heuristics that may be useful in picking up the specific skills needed for research (reading, writing, programming) and ...
Picture a computer ...This engaging book explains computation accessibly and shows its relevance to daily life. Something to think about next time we execute the algorithm of getting up in the morning.
* 演讲的目的是为了inform、demonstrate、explain或persuade听众。 二、演讲的要素 * 演讲的三要素:信息、演讲者、听众 * 信息是演讲的核心,演讲者通过语言和非语言符号传递信息。 * 演讲者是信息的来源,通过...
this article explain how to make a program in visual c++ to transmit to srial port from window 7 or window xp version service pack 2.
This tutorial explains how to set up log4j with email, files and stdout. It compares XML to properties configuration files, shows how to change LogLevels for a running application. Furthermore, we ...
Oracle 中 explain_plan 的用法 Oracle 中的 explain_plan 是一个强大的分析工具,能够帮助用户了解 SQL 语句的执行计划,通过它可以了解 Oracle 是如何连接表、使用什么方式扫描表(索引扫描或全表扫描)以及使用...
值得一看的书。看看如何写好代码
I explain new and interesting bits of syntax, but this book isn’t primarily about teaching you how to use JavaScript. It’s about teaching you how to build real applications by using AdonisJs. The ...
An Introduction to Data Science by Jeffrey S.... Readers will learn essential skills in data science through demonstrations of how to use data to construct models, predict outcomes, and visualize data.
Oracle的Explain Plan是数据库管理员和开发人员用来分析SQL查询执行计划的重要工具。它能提供关于如何执行SQL语句的详细信息,包括哪些操作(如全表扫描、索引访问等)将被使用,以及这些操作的预计成本、时间及资源...
* How to explain, coordinate, and share your efforts with others. If you’re a traditional software tester who has never written an automated test before, this is the perfect book for getting started...
We start with an overview of ERP and NetSuite ERP, before going on to explain the built-in features to show the breadth of NetSuite ERP's product and its ease of use. We then discuss business aspects...
This book will teach you what Blockchain is, how it maintains data integrity, and how to create real- world Blockchain projects using Ethereum. With interesting real-world projects, you will know ...
They explain how to have better hunches and when to leave things to chance, how to deal with overwhelming choices and how best to connect with others. From finding a spouse to finding a parking spot,...