詳解Lombok快速上手(安裝、使用與注解參數(shù))
Lombok插件安裝與使用說明
在實習(xí)中發(fā)現(xiàn)項目中IDE一直報檢查錯誤,原來是使用了Lombok注解的黑科技,這里整理了一些日常編碼中能遇到的所有關(guān)于它的使用詳解,其實lombok項目的產(chǎn)生就是為了省去我們手動創(chuàng)建getter和setter方法等等一些基本組件代碼的麻煩,它能夠在我們編譯源碼的時候自動幫我們生成getter和setter方法。即它最終能夠達到的效果是:在源碼中沒有g(shù)etter和setter等組件方法,但是在編譯生成的字節(jié)碼文件中有g(shù)etter和setter等組件方法。
常見參數(shù)
- @Setter 注解在類或字段,注解在類時為所有字段生成setter方法,注解在字段上時只為該字段生成setter方法。
- @Getter 使用方法同上,區(qū)別在于生成的是getter方法。
- @ToString 注解在類,添加toString方法。
- @EqualsAndHashCode 注解在類,生成hashCode和equals方法。
- @NoArgsConstructor 注解在類,生成無參的構(gòu)造方法。
- @RequiredArgsConstructor 注解在類,為類中需要特殊處理的字段生成構(gòu)造方法,比如final和被@NonNull注解的字段。
- @AllArgsConstructor 注解在類,生成包含類中所有字段的構(gòu)造方法。
- @Data 注解在類,為類的所有字段注解@ToString、@EqualsAndHashCode、@Getter的便捷方法,同時為所有非final字段注解@Setter。
lombok的依賴于安裝
依賴管理
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency>
IDEA插件的安裝
如果IDE沒有安裝插件的話會提示錯誤,而且不會有代碼提示,所以IDE要安裝插件
- 在IDEA插件里搜索lombok,安裝,重啟
- 直接官網(wǎng)下載插件,安裝,這是鏈接
- 設(shè)置中啟用annotation processors
@Data
@Data小例子
平時在使用時最常用@Data注解
@Data可以很好地處理字段的泛型參數(shù)。 為了在為具有泛型的類構(gòu)造對象時減少樣板,可以使用staticConstructor參數(shù)來生成私有構(gòu)造函數(shù),以及返回新實例的靜態(tài)方法。 這樣,javac將推斷變量名稱。 因此,通過這樣聲明:@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;
}
//可以看到這里自動生成了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;
}
}
}
擴展@ToString
任何類定義都可以用@ToString注釋,讓lombok生成toString()方法的實現(xiàn)。默認情況下,它會按順序打印類名以及每個字段,并以逗號分隔。
通過將includeFieldNames參數(shù)設(shè)置為true,您可以為toString()方法的輸出更詳細(但也有一些長度)。這是默認的
默認情況下,將打印所有非靜態(tài)字段。如果要跳過某些字段,可以使用@ ToString.Exclude注釋這些字段?;蛘?,您可以使用@ToString(onlyExplicitlyIncluded = true)準(zhǔn)確指定要使用的字段,然后使用@ ToString.Include標(biāo)記要包含的每個字段。
通過將callSuper設(shè)置為true,可以將toString的超類實現(xiàn)的輸出包含到輸出中。但是java.lang.Object中toString()的默認實現(xiàn)幾乎毫無意義,因此除非擴展另一個類,否則不要這樣做。
還可以在toString中包含方法調(diào)用的輸出。只能包含不帶參數(shù)的實例(非靜態(tài))方法。為此,請使用@ToString.Include標(biāo)記方法。
可以使用@ToString.Include(name =“some other name”)更改用于標(biāo)識成員的名稱,并且可以通過@ToString.Include(rank = -1)更改成員的打印順序。沒有等級的成員被認為具有等級0,更高等級的成員被首先打印,并且相同等級的成員以它們在源文件中出現(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)造器注解擴展
@NoArgsConstructor
此注釋主要與@Data或生成注釋的其他構(gòu)造函數(shù)組合使用。
@NoArgsConstructor將生成一個沒有參數(shù)的構(gòu)造函數(shù)。如果這是不可能的(因為最終字段),將導(dǎo)致編譯器錯誤,除非使用@NoArgsConstructor(force = true),然后使用0 / false / null初始化所有final字段。對于具有約束的字段,例如@NonNull字段,不會生成任何檢查,因此請注意,在稍后正確初始化這些字段之前,通常不會滿足這些約束。某些java構(gòu)造(例如hibernate和Service Provider Interface)需要no-args構(gòu)造函數(shù)。
@RequiredArgsConstructor
@RequiredArgsConstructor為每個需要特殊處理的字段生成一個帶有1個參數(shù)的構(gòu)造函數(shù)。所有未初始化的final字段都會獲得一個參數(shù),以及標(biāo)記為@NonNull的任何字段,這些字段在聲明它們時未初始化。對于標(biāo)有@NonNull的字段,還會生成顯式空檢查。如果用于標(biāo)記為@NonNull的字段的任何參數(shù)包含null,則構(gòu)造函數(shù)將拋出NullPointerException。參數(shù)的順序與字段在類中的顯示順序相匹配。
@AllArgsConstructor
@AllArgsConstructor為類中的每個字段生成一個帶有1個參數(shù)的構(gòu)造函數(shù)。標(biāo)有@NonNull的字段會導(dǎo)致對這些參數(shù)進行空檢查。
這些注釋中的每一個都允許使用替代形式,其中生成的構(gòu)造函數(shù)始終是私有的,并且生成包圍私有構(gòu)造函數(shù)的附加靜態(tài)工廠方法。通過為注釋提供staticName值來啟用此模式,如下所示:@RequiredArgsConstructor(staticName =“of”)。與普通構(gòu)造函數(shù)不同,這種靜態(tài)工廠方法將推斷泛型。這意味著您的API用戶可以編寫MapEntry.of(“foo”,5)而不是更長的新MapEntry <String,Integer>(“foo”,5)。
與大多數(shù)其他lombok注釋不同,顯式構(gòu)造函數(shù)的存在不會阻止這些注解生成自己的構(gòu)造函數(shù)。這意味著可以編寫自己的專用構(gòu)造函數(shù),并讓lombok生成樣板文件。
注意:如果出現(xiàn)沖突(自定義的一個構(gòu)造函數(shù)最終使用與lombok生成的構(gòu)造函數(shù)相同),則會發(fā)生編譯器錯誤。
@Log及其他日志注解
就是簡化了生成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");
}
}
資料鏈接
想要更詳細的了解Lombok,推薦查看它的github來閱讀更多的使用特性
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java結(jié)合redis實現(xiàn)接口防重復(fù)提交
本文主要介紹了Java結(jié)合redis實現(xiàn)接口防重復(fù)提交,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Spring項目如何實現(xiàn)帶請求鏈路id的日志記錄
我們在做項目的時候通常需要通過請求日志來排查定位線上問題,如果日志比較多而我們又需要查找整個請求的全部日志的時候會比較困難,下面我們就來看看如何用java aop實現(xiàn)請求id的日志記錄吧2024-12-12
springboot使用DynamicDataSource動態(tài)切換數(shù)據(jù)源的實現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動態(tài)切換數(shù)據(jù)源的實現(xiàn)過程,Spring Boot應(yīng)用中可以配置多個數(shù)據(jù)源,并根據(jù)注解靈活指定當(dāng)前使用的數(shù)據(jù)源,需要的朋友可以參考下2023-08-08

