Lombok快速上手(安裝、使用與註解參數)

Lombok插件安裝與使用說明

在實習中發現項目中IDE一直報檢查錯誤,原來是使用了Lombok註解的黑科技,這裏整理了一些平常編碼中能遇到的全部關於它的使用詳解,其實lombok項目的產生就是爲了省去咱們手動建立getter和setter方法等等一些基本組件代碼的麻煩,它可以在咱們編譯源碼的時候自動幫咱們生成getter和setter方法。即它最終可以達到的效果是:在源碼中沒有getter和setter等組件方法,可是在編譯生成的字節碼文件中有getter和setter等組件方法。git

常見參數

  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要安裝插件github

  1. 在IDEA插件裏搜索lombok,安裝,重啓
  2. 直接官網下載插件,安裝,這是連接
  3. 設置中啓用annotation processors

@Data小例子

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

@Data能夠很好地處理字段的泛型參數。 爲了在爲具備泛型的類構造對象時減小樣板,可使用staticConstructor參數來生成私有構造函數,以及返回新實例的靜態方法。 這樣,javac將推斷變量名稱。 所以,經過這樣聲明:@Data(staticConstructor =「of」)類Foo {private T x;}能夠經過寫入來建立Foo的新實例:Foo.of(5); 而沒必要寫:new Foo (5); 數組

若是使用了@Data註解ide

@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()方法的實現。默認狀況下,它會按順序打印類名以及每一個字段,並以逗號分隔。ui

經過將includeFieldNames參數設置爲true,您能夠爲toString()方法的輸出更詳細(但也有一些長度)。這是默認的this

默認狀況下,將打印全部非靜態字段。若是要跳過某些字段,可使用@ 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來閱讀更多的使用特性

Lombok的gitHub連接

相關文章
相關標籤/搜索