1. 程式人生 > >Lombok快速安裝與使用手冊

Lombok快速安裝與使用手冊

目錄

Lombok外掛安裝與使用說明

在實習中發現專案中IDE一直報檢查錯誤,原來是使用了Lombok註解的黑科技,這裡整理了一些日常編碼中能遇到的所有關於它的使用詳解,其實lombok專案的產生就是為了省去我們手動建立getter和setter方法等等一些基本元件程式碼的麻煩,它能夠在我們編譯原始碼的時候自動幫我們生成getter和setter方法。即它最終能夠達到的效果是:在原始碼中沒有getter和setter等元件方法,但是在編譯生成的位元組碼檔案中有getter和setter等元件方法。

常見引數

  1. @Setter 註解在類或欄位,註解在類時為所有欄位生成setter方法,註解在欄位上時只為該欄位生成setter方法。
  2. @Getter 使用方法同上,區別在於生成的是getter方法。
  3. @ToString 註解在類,新增toString方法。
  4. @EqualsAndHashCode 註解在類,生成hashCode和equals方法。
  5. @NoArgsConstructor 註解在類,生成無參的構造方法。
  6. @RequiredArgsConstructor 註解在類,為類中需要特殊處理的欄位生成構造方法,比如final和被@NonNull註解的欄位。
  7. @AllArgsConstructor 註解在類,生成包含類中所有欄位的構造方法。
  8. @Data 註解在類,為類的所有欄位註解@ToString、@EqualsAndHashCode、@Getter的便捷方法,同時為所有非final欄位註解@Setter。

lombok的依賴於安裝

依賴管理

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
</dependency>

IDEA外掛的安裝

如果IDE沒有安裝外掛的話會提示錯誤,而且不會有程式碼提示,所以IDE要安裝外掛

  1. 在IDEA外掛裡搜尋lombok,安裝,重啟
  2. 直接官網下載外掛,安裝,這是連結
  3. 設定中啟用annotation processors

@Data小例子

平時在使用時最常用@Data註解

@Data可以很好地處理欄位的泛型引數。 為了在為具有泛型的類構造物件時減少樣板,可以使用staticConstructor引數來生成私有建構函式,以及返回新例項的靜態方法。 這樣,javac將推斷變數名稱。 因此,通過這樣宣告:@Data(staticConstructor =“of”)類Foo {private T x;}可以通過寫入來建立Foo的新例項:Foo.of(5); 而不必寫:new Foo (5);

如果使用了@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()方法的實現。預設情況下,它會按順序列印類名以及每個欄位,並以逗號分隔。

通過將includeFieldNames引數設定為true,您可以為toString()方法的輸出更詳細(但也有一些長度)。這是預設的

預設情況下,將列印所有非靜態欄位。如果要跳過某些欄位,可以使用@ ToString.Exclude註釋這些欄位。或者,您可以使用@ToString(onlyExplicitlyIncluded = true)準確指定要使用的欄位,然後使用@ ToString.Include標記要包含的每個欄位。

通過將callSuper設定為true,可以將toString的超類實現的輸出包含到輸出中。但是java.lang.Object中toString()的預設實現幾乎毫無意義,因此除非擴充套件另一個類,否則不要這樣做。

還可以在toString中包含方法呼叫的輸出。只能包含不帶引數的例項(非靜態)方法。為此,請使用@ToString.Include標記方法。

可以使用@ToString.Include(name =“some other name”)更改用於標識成員的名稱,並且可以通過@ToString.Include(rank = -1)更改成員的列印順序。沒有等級的成員被認為具有等級0,更高等級的成員被首先列印,並且相同等級的成員以它們在原始檔中出現的相同順序被列印。

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) + ")";
  }
}

構造器註解擴充套件

@NoArgsConstructor

此註釋主要與@Data或生成註釋的其他建構函式組合使用。

@NoArgsConstructor將生成一個沒有引數的建構函式。如果這是不可能的(因為最終欄位),將導致編譯器錯誤,除非使用@NoArgsConstructor(force = true),然後使用0 / false / null初始化所有final欄位。對於具有約束的欄位,例如@NonNull欄位,不會生成任何檢查,因此請注意,在稍後正確初始化這些欄位之前,通常不會滿足這些約束。某些java構造(例如hibernate和Service Provider Interface)需要no-args建構函式。

@RequiredArgsConstructor

@RequiredArgsConstructor為每個需要特殊處理的欄位生成一個帶有1個引數的建構函式。所有未初始化的final欄位都會獲得一個引數,以及標記為@NonNull的任何欄位,這些欄位在宣告它們時未初始化。對於標有@NonNull的欄位,還會生成顯式空檢查。如果用於標記為@NonNull的欄位的任何引數包含null,則建構函式將丟擲NullPointerException。引數的順序與欄位在類中的顯示順序相匹配。

@AllArgsConstructor

@AllArgsConstructor為類中的每個欄位生成一個帶有1個引數的建構函式。標有@NonNull的欄位會導致對這些引數進行空檢查。

這些註釋中的每一個都允許使用替代形式,其中生成的建構函式始終是私有的,並且生成包圍私有建構函式的附加靜態工廠方法。通過為註釋提供staticName值來啟用此模式,如下所示:@RequiredArgsConstructor(staticName =“of”)。與普通建構函式不同,這種靜態工廠方法將推斷泛型。這意味著您的API使用者可以編寫MapEntry.of(“foo”,5)而不是更長的新MapEntry <String,Integer>(“foo”,5)。

與大多數其他lombok註釋不同,顯式建構函式的存在不會阻止這些註解生成自己的建構函式。這意味著可以編寫自己的專用建構函式,並讓lombok生成樣板檔案。

注意:如果出現衝突(自定義的一個建構函式最終使用與lombok生成的建構函式相同),則會發生編譯器錯誤

@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來閱讀更多的使用特性