作用
Lombok是一種Java?實(shí)用工具堵幽,可用來幫助開發(fā)人員消除Java的冗長代碼垫释,尤其是對(duì)于簡單的Java對(duì)象(POJO)拓挥。它通過注釋實(shí)現(xiàn)這一目的菇爪。通過在開發(fā)環(huán)境中實(shí)現(xiàn)Lombok算芯,開發(fā)人員可以節(jié)省構(gòu)建諸如hashCode()和equals()這樣的方法以及以往用來分類各種accessor和mutator的大量時(shí)間。
注解說明
@val
用在局部變量前面凳宙,相當(dāng)于將變量聲明為final
@NonNull
給方法參數(shù)增加這個(gè)注解會(huì)自動(dòng)在方法內(nèi)對(duì)該參數(shù)進(jìn)行是否為空的校驗(yàn)熙揍,為空拋出NPE
@Cleanup
自動(dòng)管理資源,用在局部變量之前氏涩,在當(dāng)前變量范圍內(nèi)即將執(zhí)行完畢退出之前會(huì)自動(dòng)清理資源届囚,自動(dòng)生成try-finally這樣的代碼來關(guān)閉流
@Getter/Setter
用在屬性上有梆,再也不用自己手寫setter和getter方法了,還可以指定訪問范圍
@ToString
用在類上意系,可以自動(dòng)覆寫toString方法泥耀,當(dāng)然還可以加其他參數(shù),例如@ToString(exclude=”id”)排除id屬性蛔添,或者@ToString(callSuper=true, includeFieldNames=true)調(diào)用父類的toString方法痰催,包含所有屬性
@EqualsAndHashCode
用在類上,自動(dòng)生成equals方法和hashCode方法,callSuper 是否調(diào)用父類迎瞧,exclude排除參數(shù)夸溶;of指定參數(shù)。
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
用在類上凶硅,自動(dòng)生成無參構(gòu)造和使用所有參數(shù)的構(gòu)造函數(shù)以及把所有@NonNull屬性作為參數(shù)的構(gòu)造函數(shù)缝裁,如果指定staticName = “of”參數(shù),同時(shí)還會(huì)生成一個(gè)返回類對(duì)象的靜態(tài)工廠方法咏尝,比使用構(gòu)造函數(shù)方便很多压语、
@Data
注解在類上,相當(dāng)于同時(shí)使用了@ToString编检、@EqualsAndHashCode胎食、@Getter、@Setter和@RequiredArgsConstrutor這些注解允懂,對(duì)于POJO類十分有用
@Value
用在類上厕怜,是@Data的不可變形式,相當(dāng)于為屬性添加final聲明蕾总,只提供getter方法粥航,而不提供setter方法
@Builder
用在類、構(gòu)造器生百、方法上递雀,為你提供復(fù)雜的builder APIs
@SneakyThrows
自動(dòng)拋受檢異常,而無需顯式在方法上使用throws語句*
@Synchronized
用在方法上蚀浆,將方法聲明為同步的缀程,并自動(dòng)加鎖,而鎖對(duì)象是一個(gè)私有的屬性$lock或$LOCK市俊,而java中的synchronized關(guān)鍵字鎖對(duì)象是this杨凑,鎖在this或者自己的類對(duì)象上存在副作用,就是你不能阻止非受控代碼去鎖this或者類對(duì)象摆昧,這可能會(huì)導(dǎo)致競爭條件或者其它線程錯(cuò)誤
@Getter(lazy=true)
可以替代經(jīng)典的Double Check Lock樣板代碼
@Log
根據(jù)不同的注解生成不同類型的log對(duì)象撩满,但是實(shí)例名稱都是log,有六種可選實(shí)現(xiàn)類
* @CommonsLog
Creates log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
* @Log
Creates log = java.util.logging.Logger.getLogger(LogExample.class.getName());
* @Log4j
Creates log = org.apache.log4j.Logger.getLogger(LogExample.class);
* @Log4j2
Creates log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
* @Slf4j
Creates log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
* @XSlf4j
Creates log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
部分實(shí)例
1.value
@Value
public class LombokDemo {
@NonNull
private int id;
@NonNull
private String shap;
private int age;
//相當(dāng)于
private final int id;
public int getId() {
return this.id;
}
...
}
2.構(gòu)造函數(shù)
@NoArgsConstructor
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor
public class LombokDemo {
@NonNull
private int id;
@NonNull
private String shap;
private int age;
public static void main(String[] args) {
new LombokDemo(1, "circle");
//使用靜態(tài)工廠方法
LombokDemo.of(2, "circle");
//無參構(gòu)造
new LombokDemo();
//包含所有參數(shù)
new LombokDemo(1, "circle", 2);
}
}
3.CleanUp
public static void main(String[] args) {
try {
@Cleanup InputStream inputStream = new FileInputStream(args[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//=>相當(dāng)于
InputStream inputStream = null;
try {
inputStream = new FileInputStream(args[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.val
public static void main(String[] args) {
val sets = new HashSet<String>();
val lists = new ArrayList<String>();
val maps = new HashMap<String, String>();
//=>相當(dāng)于如下
final Set<String> sets2 = new HashSet<>();
final List<String> lists2 = new ArrayList<>();
final Map<String, String> maps2 = new HashMap<>();
}