Lombok使用


1. Lombok簡介

Lombok是一個可以通過簡單的注解形式來幫助我們簡化消除一些必要但臃腫的Java代碼(如getter/setter/toString等)的工具,通過使用對應的注解,可以在編譯源碼的時候生成對應的方法,從而減少大量重復代碼的書寫。

2. 使用方法

在IDEA中使用,首先安裝插件,這樣才能在添加注解之后看到類的改變

之后引入jar包即可

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
    <scope>provided</scope>
</dependency>

3. 相關注解

官方注解的介紹:https://projectlombok.org/features/all

3.1 @Getter and @Setter

你可以用@Getter / @Setter注釋任何字段(當然也可以注釋到類上的),讓lombok自動生成默認的getter / setter方法,生成的getter遵循布爾屬性的約定。
默認生成的方法是public的,如果要修改方法修飾符可以設置AccessLevel的值,例如:@Getter(access = AccessLevel.PROTECTED)

字段上的注解示例:

類上的注解示例:(使用類上的注解時,會默認生成一個無參構造)

3.2 @ToString

生成toString()方法,默認情況下,它會按順序(以逗號分隔)打印你的類名稱以及每個字段。
  • 可以這樣設置不包含哪些字段,例如:@ToString(exclude = "id") / @ToString(exclude = {"id","name"})
  • 如果繼承的有父類的話,可以設置callSuper 讓其調用父類的toString()方法,例如:@ToString(callSuper = true)
  • 如果需要可以通過注釋參數includeFieldNames來控制輸出中是否包含的屬性名稱。,例如:@ToString(includeFieldNames= false)

上面添加的注解映射到代碼中為

 @Override public String toString() { return "User{" +
                "name='" + name + '\'' +
                '}'; }

3.3 @EqualsAndHashCode

生成hashCode()和equals()方法,默認情況下,它將使用所有非靜態,非transient字段。但可以通過在可選的exclude參數中來排除更多字段。或者,通過在parameter參數中命名它們來准確指定希望使用哪些字段。

 import lombok.EqualsAndHashCode; @EqualsAndHashCode public class EqualsAndHashCodeExample { private transient int transientVar = 10; private String name; private double score; @EqualsAndHashCode.Exclude private Shape shape = new Square(5, 10); private String[] tags; @EqualsAndHashCode.Exclude private int id; public String getName() { return this.name; } @EqualsAndHashCode(callSuper=true) public static class Square extends Shape { private final int width, height; public Square(int width, int height) { this.width = width; this.height = height; } } }

映射到代碼中為

import java.util.Arrays; public class EqualsAndHashCodeExample { private transient int transientVar = 10; private String name; private double score; private Shape shape = new Square(5, 10); private String[] tags; private int id; public String getName() { return this.name; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof EqualsAndHashCodeExample)) return false; EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false; if (Double.compare(this.score, other.score) != 0) return false; if (!Arrays.deepEquals(this.tags, other.tags)) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final long temp1 = Double.doubleToLongBits(this.score); result = (result*PRIME) + (this.name == null ? 43 : this.name.hashCode()); result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32)); result = (result*PRIME) + Arrays.deepHashCode(this.tags); return result; } protected boolean canEqual(Object other) { return other instanceof EqualsAndHashCodeExample; } 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 boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Square)) return false; Square other = (Square) o; if (!other.canEqual((Object)this)) return false; if (!super.equals(o)) return false; if (this.width != other.width) return false; if (this.height != other.height) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; result = (result*PRIME) + super.hashCode(); result = (result*PRIME) + this.width; result = (result*PRIME) + this.height; return result; } protected boolean canEqual(Object other) { return other instanceof Square; } } }

3.4 @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

3.4.1 @NoArgsConstructor

@NoArgsConstructor生成一個無參構造方法。當類中有final字段沒有被初始化時,編譯器會報錯,此時可用@NoArgsConstructor(force = true),然后就會為沒有初始化的final字段設置默認值 0 / false / null。對於具有約束的字段(例如@NonNull字段),不會生成檢查或分配,因此請注意,正確初始化這些字段之前,這些約束無效。

3.4.2 @RequiredArgsConstructor

@RequiredArgsConstructor會生成構造方法(可能帶參數也可能不帶參數),如果帶參數,這參數只能是以final修飾的未經初始化的字段,或者是以@NonNull注解的未經初始化的字段

@RequiredArgsConstructor(staticName = "of")會生成一個of()的靜態方法,並把構造方法設置為私有的

3.4.3 @AllArgsConstructor

@AllArgsConstructor 生成一個全參數的構造方法,默認不提供無參構造。

3.5 @Data 

該注解使用在類上,該注解會提供getter、setter、equals、canEqual、hashCode、toString方法。

雖然@Data注解非常有用,但是它沒有與其他注解相同的控制粒度。@Data提供了一個可以生成靜態工廠的單一參數,將staticConstructor參數設置為所需要的名稱,Lombok自動生成的構造函數設置為私有,並提供公開的給定名稱的靜態工廠方法。 

 

3.6 @NonNull

該注解使用在屬性上,該注解用於屬性的非空檢查,當放在setter方法的字段上,將生成一個空檢查,如果為空,則拋出NullPointerException

3.7 @Accessors

@Accessors 主要用於控制生成的getter和setter

  • fluent boolean值,默認為false。此字段主要為控制生成的getter和setter方法前面是否帶get/set
  • chain boolean值,默認false。如果設置為true,setter返回的是此對象,方便鏈式調用方法
  • prefix 設置前綴 例如:@Accessors(prefix = "my") private String myName  當生成get/set方法時,會把此前綴去掉

 

3.8 @Value

這個注解用在 類 上,會生成含所有參數的構造方法,get 方法,此外還提供了equals、hashCode、toString 方法。 
注意:沒有setter 方法

3.9 @Synchronized

該注解使用在類或者實例方法上,Synchronized在一個方法上,使用關鍵字可能會導致結果和想要的結果不同,因為多線程情況下會出現異常情況。Synchronized 
關鍵字將在this示例方法情況下鎖定當前對象,或者class講台方法的對象上多鎖定。這可能會導致死鎖現象。一般情況下建議鎖定一個專門用於此目的的獨立鎖,而不是允許公共對象進行鎖定。該注解也是為了達到該目的。 

 // 注解方法
@Synchronized
  public int answerToLife() {
    return 42;
  }

//等同於加鎖
public int answerToLife() {
    synchronized($lock) {
      return 42;
    }
  }

3.10 @SneakyThrows

該注解使用在方法上,這個注解用在 方法 上,可以將方法中的代碼用 try-catch 語句包裹起來,捕獲異常並在 catch 中用 Lombok.sneakyThrow(e) 把異常拋出,可以使用 @SneakyThrows(Exception.class) 的形式指定拋出哪種異常。該注解需要謹慎使用。

3.11 @Cleanup

該注解使用在屬性前,該注解是用來保證分配的資源被釋放。在本地變量上使用該注解,任何后續代碼都將封裝在try/finally中,確保當前作用於中的資源被釋放。默認@Cleanup清理的方法為close,可以使用value指定不同的方法名稱。 

3.12 @Wither

提供了給final字段賦值的一種方法

 官方解釋

3.13 @onXxx

在注解里面添加注解的方式

import lombok.Getter;
import lombok.Setter;public class User {
    
    @Getter(onMethod = @_({@Id,@Column(name="id",nullable=false),@GeneratedValue(strategy= GenerationType.AUTO)}))
    @Setter
    private Integer id;
    
    @Getter(onMethod = @_(@Column(name="age")))
    @Setter
    private int age;
    
    @Getter(onMethod = @_(@Column(name="name")))
    @Setter
    private String name;
}

3.14 @Delegate

給類生成一些列的方法,這些方法都來自於List接口

3.15 @Builder

@Builder注釋為你的類生成復雜的構建器API。

官方解釋

3.16 @Log

注解於類上,用於滿足不同的日志系統的日志使用,提供以下供選擇

//@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
//@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
//@Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
//@Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
//@Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
//@Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
//@XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM