Lombok插件安裝與使用說明
在實習中發現項目中IDE一直報檢查錯誤,原來是使用了Lombok注解的黑科技,這里整理了一些日常編碼中能遇到的所有關於它的使用詳解,其實lombok項目的產生就是為了省去我們手動創建getter和setter方法等等一些基本組件代碼的麻煩,它能夠在我們編譯源碼的時候自動幫我們生成getter和setter方法。即它最終能夠達到的效果是:在源碼中沒有getter和setter等組件方法,但是在編譯生成的字節碼文件中有getter和setter等組件方法。
常見參數
- @Setter 注解在類或字段,注解在類時為所有字段生成setter方法,注解在字段上時只為該字段生成setter方法。
- @Getter 使用方法同上,區別在於生成的是getter方法。
- @ToString 注解在類,添加toString方法。
- @EqualsAndHashCode 注解在類,生成hashCode和equals方法。
- @NoArgsConstructor 注解在類,生成無參的構造方法。
- @RequiredArgsConstructor 注解在類,為類中需要特殊處理的字段生成構造方法,比如final和被@NonNull注解的字段。
- @AllArgsConstructor 注解在類,生成包含類中所有字段的構造方法。
- @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,安裝,重啟
- 直接官網下載插件,安裝,這是鏈接
- 設置中啟用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來閱讀更多的使用特性
