POJO(Plain Ordinary Java Object)即普通Java類,具有一部分getter/setter方法的那種類就可以稱作POJO。
- 有一些private的參數作爲對象的屬性,然後針對每一個參數定義get和set方法訪問的接口。
- 沒有從任何類繼承、也沒有實現任何接口,更沒有被其它框架侵入的java對象。
JavaBean 是一種JAVA語言寫成的可重用組件。JavaBean符合一定規範編寫的Java類,不是一種技術,而是一種規範。大家針對這種規範,總結了很多開發技巧、工具函數。符合這種規範的類,可以被其它的程序或者框架使用。JavaBean的任務就是: “Write once, run anywhere, reuse everywhere”,即“一次性編寫,任何地方執行,任何地方重用”。它的方法命名,構造及行爲必須符合特定的約定:
- 所有屬性爲private
- 這個類必須有一個公共的缺省構造函數。即是提供無參數的構造器
- 這個類的屬性使用getter和setter來訪問,其他方法遵從標準命名規範
- 這個類應是可序列化的。實現serializable接口
因爲這些要求主要是靠約定而不是靠實現接口,所以許多開發者把JavaBean看作遵從特定命名約定的POJO。
兩者之間的區別
- POJO其實是比javabean更純淨的簡單類或接口。POJO嚴格地遵守簡單對象的概念,而一些JavaBean中往往會封裝一些簡單邏輯。
- POJO主要用於數據的臨時傳遞,它只能裝載數據, 作爲數據存儲的載體,而不具有業務邏輯處理的能力。
- Javabean雖然數據的獲取與POJO一樣,但是javabean當中可以有其它的方法。
我們的項目通常的是分層結構的,所以POJO有許多。
例如(這裏用lombok的@Data註解省略了屬性的getter/setter方法):
import lombok.Data; @Data public class User implements Serializable { private String id; private String name; private Integer age; }
下面來看一個JavaBean的示例。對於頁面分頁,大家都不陌生。如下是一個分頁組件示例:
import java.util.Collections; import java.util.List; import java.util.function.Function; /** * 分頁組件 * @author hongfei.guo */ public class MyPage<T> implements java.io.Serializable { private static final long serialVersionUID = 8545996863226528798L; private List<T> records; private long total; private long size; private long current; private boolean isSearchCount; public MyPage() { this.records = Collections.emptyList(); this.total = 0L; this.size = 10L; this.current = 1L; this.isSearchCount = true; } public MyPage(long current, long size) { this(current, size, 0L); } public MyPage(long current, long size, long total) { this(current, size, total, true); } public MyPage(long current, long size, boolean isSearchCount) { this(current, size, 0L, isSearchCount); } public MyPage(long current, long size, long total, boolean isSearchCount) { this.records = Collections.emptyList(); this.total = 0L; this.size = 10L; this.current = 1L; this.isSearchCount = true; if (current > 1L) { this.current = current; } this.size = size; this.total = total; this.isSearchCount = isSearchCount; } public <S> MyPage(MyIPage<S> sourcePage, Function<T,S> transFun) { this.records = Collections.emptyList(); this.total = sourcePage.getTotal(); this.size = sourcePage.getSize(); this.current = sourcePage.getCurrent(); this.isSearchCount = true; final List records = sourcePage.getRecords(); if (records != null) { for (Object record : records) { transFun.apply((T) record); } } } public boolean hasPrevious() { return this.current > 1L; } public boolean hasNext() { return this.current < this.getPages(); } public List<T> getRecords() { return this.records; } public MyPage<T> setRecords(List<T> records) { this.records = records; return this; } public long getTotal() { return this.total; } public long getSize() { return this.size; } public long getCurrent() { return this.current; } public MyPage<T> setCurrent(long current) { this.current = current; return this; } public boolean isSearchCount() { return this.total < 0L ? false : this.isSearchCount; } }
另外,在DDD領域驅動設計中,領域模型都是JavaBean。