lombok常用的註解學習筆記
技術標籤:lombok
目錄標題
一、lombok是什麼?
lombok是一種java的使用工具,可以用來幫助開發人員消除java中的冗長程式碼,尤其是對於簡單的java物件(pojo),他通過註解實現這一目的。
二、lombok在idea中的使用
1、需要在idea中安裝lombok外掛然後重啟
2、在pom檔案中引入對應的依賴
<!-- lombok依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope >provided</scope>
</dependency>
三、lombok的常用註解
1、@Getter和@Setter
這兩個註釋可以註釋在類上面,也可以註釋在欄位上面,這兩個註釋就是可以減少get和set方法的手動書寫,當然在註釋後面可以新增引數,可以說明這個欄位或者類的級別,比如@Getter(AccessLevel.PROTECTED)就是說明它是protected級別的 如果在一個類上面註釋了這兩個註解是,可是你又希望有一個欄位不會生成get和set方法是,可以在你希望不會的生成的欄位上註釋@Getter(AccessLevel.NONE),get同理,這個會把該欄位的保護起來,不會生成這兩個方法 特別注意的是如果一個欄位有靜態修飾詞static是不會生成這兩個方法,還是final修飾時,不會生成set方法。 下面是.java檔案以及生成的.class檔案
.java檔案
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
/**
* @author weiguorong
* @date 2021/2/12 16:22
*/
@Getter
@Setter
public class Student {
static int id;
final int id2 = 10;
private String name;
@Setter(AccessLevel.PRIVATE)
private String sex;
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private String email;
}
生成的.class檔案
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class Student {
static int id;
final int id2 = 10;
private String name;
private String sex;
private String email;
public Student() {
}
public int getId2() {
this.getClass();
return 10;
}
public String getName() {
return this.name;
}
public String getSex() {
return this.sex;
}
public void setName(String name) {
this.name = name;
}
private void setSex(String sex) {
this.sex = sex;
}
}
2、@ToString
這個註解不支援在欄位上,只能在類上,顧名思義就是生成toSting方法,在這邊要注意的是該註解的有兩種引數,exclude和of
用法如下@ToString(exclude = {“email”},of = {“name”}),exclude是除了大括號裡的值,其他都有,而of只有大括號裡的值
3、@EqualsAndHashCode
這個註解生成了equals、canEqual、hashCode三個方法,它個@ToString一樣有exclude和of兩個引數,用法也跟它一樣
4、@NonNull
@NonNull這個註解可以用在欄位上,也可以用在方法的引數上,作用是自動幫你判斷是否是null,如果是null,就會報空指標異常,
5、@NoArgsConstructor、@RequiredArgsConstructor和@AllArgsConstructor
這三個註解都是生成建構函式的,@NoArgsConstructor是無參的建構函式,
@RequiredArgsConstructor是構造有@NonNull修飾的欄位或者是final修飾的欄位的構造方法,@AllArgsConstructor則是所有的欄位的建構函式
6、@Data
@Data註解集成了@Getter和@Setter、@ToString、@EqualsAndHashCode、@RequiredArgsConstructor這幾個註解與一身,是一個非常強大的註解,這個註解更可以簡化註解的程式碼量
7、@Builder
@Builder註解會生成一個Builder內部類,使用的賦值操作會比之前更加方便,具體我把程式碼放下面
.java檔案
import lombok.*;
/**
* @author weiguorong
* @date 2021/2/12 16:22
*/
@Data
@Builder
public class Student {
static int id;
final int id2 = 10;
private String name;
@Setter(AccessLevel.PRIVATE)
private String sex;
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private String email;
public static void main(String[] args) {
Student student = Student.builder()
.email("123")
.sex("男")
.name("蕭炎")
.build();
System.out.println(student.toString());
}
}
生成的.class檔案
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class Student {
static int id;
final int id2 = 10;
private String name;
private String sex;
private String email;
public static void main(String[] args) {
Student student = builder().email("123").sex("男").name("蕭炎").build();
System.out.println(student.toString());
}
Student(String name, String sex, String email) {
this.name = name;
this.sex = sex;
this.email = email;
}
public static Student.StudentBuilder builder() {
return new Student.StudentBuilder();
}
public int getId2() {
this.getClass();
return 10;
}
public String getName() {
return this.name;
}
public String getSex() {
return this.sex;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Student)) {
return false;
} else {
Student other = (Student)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId2() != other.getId2()) {
return false;
} else {
label49: {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
break label49;
}
} else if (this$name.equals(other$name)) {
break label49;
}
return false;
}
Object this$sex = this.getSex();
Object other$sex = other.getSex();
if (this$sex == null) {
if (other$sex != null) {
return false;
}
} else if (!this$sex.equals(other$sex)) {
return false;
}
Object this$email = this.email;
Object other$email = other.email;
if (this$email == null) {
if (other$email != null) {
return false;
}
} else if (!this$email.equals(other$email)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Student;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getId2();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $sex = this.getSex();
result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
Object $email = this.email;
result = result * 59 + ($email == null ? 43 : $email.hashCode());
return result;
}
public String toString() {
return "Student(id2=" + this.getId2() + ", name=" + this.getName() + ", sex=" + this.getSex() + ", email=" + this.email + ")";
}
private void setSex(String sex) {
this.sex = sex;
}
public static class StudentBuilder {
private String name;
private String sex;
private String email;
StudentBuilder() {
}
public Student.StudentBuilder name(String name) {
this.name = name;
return this;
}
public Student.StudentBuilder sex(String sex) {
this.sex = sex;
return this;
}
public Student.StudentBuilder email(String email) {
this.email = email;
return this;
}
public Student build() {
return new Student(this.name, this.sex, this.email);
}
public String toString() {
return "Student.StudentBuilder(name=" + this.name + ", sex=" + this.sex + ", email=" + this.email + ")";
}
}
}
注意看.java檔案的mian方法中的賦值,這樣的鏈式賦值相比之前更方便簡潔,控制檯的輸出如下:
8、@Log
@Log註解也是簡化了日誌,可以通過註解直接獲取到日誌
9、val
這不是一個註解,它就相當於前端的var一樣,比如java中本來書寫的Map<String,String> map = new HashMap<String,String>();可以書寫成val map = new HashMap<String,String>();雖然是這樣書寫的,但是最後生成的型別還是Map<String,String>
10、@Cleanup
該註解標記與檔案流物件上,可以解決檔案流物件對異常的捕捉和檔案流的關閉的繁瑣程式碼