詳解Lombok快速上手(安裝、使用與注解參數(shù))
Lombok插件安裝與使用說(shuō)明
在實(shí)習(xí)中發(fā)現(xiàn)項(xiàng)目中IDE一直報(bào)檢查錯(cuò)誤,原來(lái)是使用了Lombok注解的黑科技,這里整理了一些日常編碼中能遇到的所有關(guān)于它的使用詳解,其實(shí)lombok項(xiàng)目的產(chǎn)生就是為了省去我們手動(dòng)創(chuàng)建getter和setter方法等等一些基本組件代碼的麻煩,它能夠在我們編譯源碼的時(shí)候自動(dòng)幫我們生成getter和setter方法。即它最終能夠達(dá)到的效果是:在源碼中沒(méi)有g(shù)etter和setter等組件方法,但是在編譯生成的字節(jié)碼文件中有g(shù)etter和setter等組件方法。
常見(jiàn)參數(shù)
- @Setter 注解在類或字段,注解在類時(shí)為所有字段生成setter方法,注解在字段上時(shí)只為該字段生成setter方法。
- @Getter 使用方法同上,區(qū)別在于生成的是getter方法。
- @ToString 注解在類,添加toString方法。
- @EqualsAndHashCode 注解在類,生成hashCode和equals方法。
- @NoArgsConstructor 注解在類,生成無(wú)參的構(gòu)造方法。
- @RequiredArgsConstructor 注解在類,為類中需要特殊處理的字段生成構(gòu)造方法,比如final和被@NonNull注解的字段。
- @AllArgsConstructor 注解在類,生成包含類中所有字段的構(gòu)造方法。
- @Data 注解在類,為類的所有字段注解@ToString、@EqualsAndHashCode、@Getter的便捷方法,同時(shí)為所有非final字段注解@Setter。
lombok的依賴于安裝
依賴管理
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency>
IDEA插件的安裝
如果IDE沒(méi)有安裝插件的話會(huì)提示錯(cuò)誤,而且不會(huì)有代碼提示,所以IDE要安裝插件
- 在IDEA插件里搜索lombok,安裝,重啟
- 直接官網(wǎng)下載插件,安裝,這是鏈接
- 設(shè)置中啟用annotation processors
@Data
@Data小例子
平時(shí)在使用時(shí)最常用@Data注解
@Data可以很好地處理字段的泛型參數(shù)。 為了在為具有泛型的類構(gòu)造對(duì)象時(shí)減少樣板,可以使用staticConstructor參數(shù)來(lái)生成私有構(gòu)造函數(shù),以及返回新實(shí)例的靜態(tài)方法。 這樣,javac將推斷變量名稱。 因此,通過(guò)這樣聲明:@Data(staticConstructor =“of”)類Foo
如果使用了@Data注解
@Data public class DataExample {
private final String name;
@Setter(AccessLevel.PACKAGE) private int age;
private double score;
private String[] tags;
@ToString(includeFieldNames=true)
@Data(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private final T value;
}
}
不加lombok注解的Pojo的寫法
public class DataExample {
private final String name;
private int age;
private double score;
private String[] tags;
public DataExample(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return this.score;
}
public String[] getTags() {
return this.tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override public String toString() {
return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
}
protected boolean canEqual(Object other) {
return other instanceof DataExample;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof DataExample)) return false;
DataExample other = (DataExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (this.getAge() != other.getAge()) return false;
if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + this.getAge();
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
return result;
}
public static class Exercise<T> {
private final String name;
private final T value;
private Exercise(String name, T value) {
this.name = name;
this.value = value;
}
//可以看到這里自動(dòng)生成了of方法
public static <T> Exercise<T> of(String name, T value) {
return new Exercise<T>(name, value);
}
public String getName() {
return this.name;
}
public T getValue() {
return this.value;
}
@Override public String toString() {
return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
}
protected boolean canEqual(Object other) {
return other instanceof Exercise;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Exercise)) return false;
Exercise<?> other = (Exercise<?>) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
return result;
}
}
}
擴(kuò)展@ToString
任何類定義都可以用@ToString注釋,讓lombok生成toString()方法的實(shí)現(xiàn)。默認(rèn)情況下,它會(huì)按順序打印類名以及每個(gè)字段,并以逗號(hào)分隔。
通過(guò)將includeFieldNames參數(shù)設(shè)置為true,您可以為toString()方法的輸出更詳細(xì)(但也有一些長(zhǎng)度)。這是默認(rèn)的
默認(rèn)情況下,將打印所有非靜態(tài)字段。如果要跳過(guò)某些字段,可以使用@ ToString.Exclude注釋這些字段?;蛘撸梢允褂聾ToString(onlyExplicitlyIncluded = true)準(zhǔn)確指定要使用的字段,然后使用@ ToString.Include標(biāo)記要包含的每個(gè)字段。
通過(guò)將callSuper設(shè)置為true,可以將toString的超類實(shí)現(xiàn)的輸出包含到輸出中。但是java.lang.Object中toString()的默認(rèn)實(shí)現(xiàn)幾乎毫無(wú)意義,因此除非擴(kuò)展另一個(gè)類,否則不要這樣做。
還可以在toString中包含方法調(diào)用的輸出。只能包含不帶參數(shù)的實(shí)例(非靜態(tài))方法。為此,請(qǐng)使用@ToString.Include標(biāo)記方法。
可以使用@ToString.Include(name =“some other name”)更改用于標(biāo)識(shí)成員的名稱,并且可以通過(guò)@ToString.Include(rank = -1)更改成員的打印順序。沒(méi)有等級(jí)的成員被認(rèn)為具有等級(jí)0,更高等級(jí)的成員被首先打印,并且相同等級(jí)的成員以它們?cè)谠次募谐霈F(xiàn)的相同順序被打印。
ToString小例子:
加注解:
@ToString
public class ToStringExample {
private static final int STATIC_VAR = 10;
private String name;
private Shape shape = new Square(5, 10);
private String[] tags;
@ToString.Exclude private int id;
public String getName() {
return this.name;
}
@ToString(callSuper=true, includeFieldNames=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}
等效于:
public class ToStringExample {
private static final int STATIC_VAR = 10;
private String name;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.getName();
}
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
@Override public String toString() {
return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
}
}
@Override public String toString() {
return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
}
}
構(gòu)造器注解擴(kuò)展
@NoArgsConstructor
此注釋主要與@Data或生成注釋的其他構(gòu)造函數(shù)組合使用。
@NoArgsConstructor將生成一個(gè)沒(méi)有參數(shù)的構(gòu)造函數(shù)。如果這是不可能的(因?yàn)樽罱K字段),將導(dǎo)致編譯器錯(cuò)誤,除非使用@NoArgsConstructor(force = true),然后使用0 / false / null初始化所有final字段。對(duì)于具有約束的字段,例如@NonNull字段,不會(huì)生成任何檢查,因此請(qǐng)注意,在稍后正確初始化這些字段之前,通常不會(huì)滿足這些約束。某些java構(gòu)造(例如hibernate和Service Provider Interface)需要no-args構(gòu)造函數(shù)。
@RequiredArgsConstructor
@RequiredArgsConstructor為每個(gè)需要特殊處理的字段生成一個(gè)帶有1個(gè)參數(shù)的構(gòu)造函數(shù)。所有未初始化的final字段都會(huì)獲得一個(gè)參數(shù),以及標(biāo)記為@NonNull的任何字段,這些字段在聲明它們時(shí)未初始化。對(duì)于標(biāo)有@NonNull的字段,還會(huì)生成顯式空檢查。如果用于標(biāo)記為@NonNull的字段的任何參數(shù)包含null,則構(gòu)造函數(shù)將拋出NullPointerException。參數(shù)的順序與字段在類中的顯示順序相匹配。
@AllArgsConstructor
@AllArgsConstructor為類中的每個(gè)字段生成一個(gè)帶有1個(gè)參數(shù)的構(gòu)造函數(shù)。標(biāo)有@NonNull的字段會(huì)導(dǎo)致對(duì)這些參數(shù)進(jìn)行空檢查。
這些注釋中的每一個(gè)都允許使用替代形式,其中生成的構(gòu)造函數(shù)始終是私有的,并且生成包圍私有構(gòu)造函數(shù)的附加靜態(tài)工廠方法。通過(guò)為注釋提供staticName值來(lái)啟用此模式,如下所示:@RequiredArgsConstructor(staticName =“of”)。與普通構(gòu)造函數(shù)不同,這種靜態(tài)工廠方法將推斷泛型。這意味著您的API用戶可以編寫MapEntry.of(“foo”,5)而不是更長(zhǎng)的新MapEntry <String,Integer>(“foo”,5)。
與大多數(shù)其他lombok注釋不同,顯式構(gòu)造函數(shù)的存在不會(huì)阻止這些注解生成自己的構(gòu)造函數(shù)。這意味著可以編寫自己的專用構(gòu)造函數(shù),并讓lombok生成樣板文件。
注意:如果出現(xiàn)沖突(自定義的一個(gè)構(gòu)造函數(shù)最終使用與lombok生成的構(gòu)造函數(shù)相同),則會(huì)發(fā)生編譯器錯(cuò)誤。
@Log及其他日志注解
就是簡(jiǎn)化了生成log的代碼,直接看例子
@Log
public class LogExample {
public static void main(String... args) {
log.severe("Something's wrong here");
}
}
@Slf4j
public class LogExampleOther {
public static void main(String... args) {
log.error("Something else is wrong here");
}
}
@CommonsLog(topic="CounterLog")
public class LogExampleCategory {
public static void main(String... args) {
log.error("Calling the 'CounterLog' with a message");
}
}
等效于:
public class LogExample {
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
public static void main(String... args) {
log.severe("Something's wrong here");
}
}
public class LogExampleOther {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);
public static void main(String... args) {
log.error("Something else is wrong here");
}
}
public class LogExampleCategory {
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog");
public static void main(String... args) {
log.error("Calling the 'CounterLog' with a message");
}
}
資料鏈接
想要更詳細(xì)的了解Lombok,推薦查看它的github來(lái)閱讀更多的使用特性
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java結(jié)合redis實(shí)現(xiàn)接口防重復(fù)提交
本文主要介紹了Java結(jié)合redis實(shí)現(xiàn)接口防重復(fù)提交,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Java實(shí)現(xiàn)登錄與注冊(cè)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)登錄與注冊(cè)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Spring項(xiàng)目如何實(shí)現(xiàn)帶請(qǐng)求鏈路id的日志記錄
我們?cè)谧鲰?xiàng)目的時(shí)候通常需要通過(guò)請(qǐng)求日志來(lái)排查定位線上問(wèn)題,如果日志比較多而我們又需要查找整個(gè)請(qǐng)求的全部日志的時(shí)候會(huì)比較困難,下面我們就來(lái)看看如何用java aop實(shí)現(xiàn)請(qǐng)求id的日志記錄吧2024-12-12
springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程
這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程,Spring Boot應(yīng)用中可以配置多個(gè)數(shù)據(jù)源,并根據(jù)注解靈活指定當(dāng)前使用的數(shù)據(jù)源,需要的朋友可以參考下2023-08-08
idea切換分支的時(shí)候,忽略一些無(wú)用的修改設(shè)置
這篇文章主要介紹了idea切換分支的時(shí)候,忽略一些無(wú)用的修改操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

