**
lombok 註解
**
lombok 提供的註解不多,可以參考官方視頻的講解和官方文檔。
Lombok 註解在線幫助文檔:https://projectlombok.org/features/index.html
下面介紹幾個我常用的 lombok 註解:
@Data :註解在類上;提供類所有屬性的 getting 和 setting 方法,此外還提供了equals、canEqual、hashCode、toString 方法
@Setter:註解在屬性上;爲屬性提供 setting 方法
@Getter:註解在屬性上;爲屬性提供 getting 方法
@Log4j :註解在類上;爲類提供一個 屬性名爲log 的 log4j 日誌對象
@NoArgsConstructor:註解在類上;爲類提供一個無參的構造方法
@AllArgsConstructor:註解在類上;爲類提供一個全參的構造方法
@NonNull:註解在參數上,可以省略重複的 if( null == persion)這類異常處理
@Cleanup:註解在輸入輸出流等需要釋放資源的變量上,不需要寫額外繁瑣而重複的釋放資源代碼
來一個最簡單的案例:
不用Lombok
public class Person {
private String id;
private String name;
private String identity;
private Logger log = Logger.getLogger(Person.class);
public Person() {
}
public Person(String id, String name, String identity) {
this.id = id;
this.name = name;
this.identity = identity;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getIdentity() {
return identity;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setIdentity(String identity) {
this.identity = identity;
}
}
使用Lombok
@Data
@Log4j
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private String id;
private String name;
private String identity;
}