Java:Lombok插件用法筆記

1、Lombok是什么東東?

官方介紹Lombok項目是一個Java庫,它可以自動嵌入你的編輯器和構建工具中,從而減少你的代碼量。永遠不要再寫另一個getter或equals方法,它帶有一個注釋的你的類有一個功能全面的生成器,自動化你的日志記錄變量等等功能。簡單來說就是使用Lombok,通過注解,讓你不再需要編寫getter、equals等屬性方法,減少樣板代碼的編寫、起到提升代碼效率的功能。

2、IDEA如何安裝Lombok

IDEA開發工具如果需要正常使用Lombok,就需要安裝Lombok插件,這樣IDEA就可以正常識別Lombok注解,從而可以正常編譯項目。今天給大家介紹一下如何通過IDEA安裝IDEA插件。

安裝方法:

1、工具欄點擊File→Settings設置界面→找到Plugins→找到Lombok插件然后點擊install→重啟IDEA,安裝Lombok插件如下圖:

2、點擊File-- Settings設置界面,開啟 AnnocationProcessors如下圖:

開啟 AnnocationProcessors目的是讓Lombok注解在編譯階段起作用。

3、如何使用Lombok

3.1 添加依賴

<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

4 、 Lombok常用用法示例

今天主要給大家羅列一下一些比較常用的lombok用法。

4.1 @Getter/@Setter

public class User {@Getter @Setterprivate Long id;
@Getter(AccessLevel.PROTECTED)private String phone;
private String password;}//編譯后的代碼public class User {private Long id;private String phone;private String password;public User() {    }public Long getId() {return this.id;    }public void setId(Long id) {this.id = id;    }protected String getPhone() {return this.phone;    }}

說明:@Getter @Setter 注解在類上,表示為類中的所有字段生成Getter&Setter方法。也可以使用@Data來代替@Getter @Setter,但是@Data會引入更多的注解。

4.2 @NonNull

作用:給字段賦值時(調用字段的setter方法時),如果傳遞的參數值為null,則會拋出空異常NullPointerException,生成setter方法時會對參數是否為空進行檢查。

@Getter@Setterpublic class User {private Long id;@NonNullprivate String phone;}//編譯后生成的代碼public class User {private Long id;@NonNullprivate String phone;
public User() {    }public Long getId() {return this.id;    }public void setId(Long id) {this.id = id;    }@NonNullpublic String getPhone() {return this.phone;    }public void setPhone(@NonNull String phone) {if(phone == null) {throw new NullPointerException("phone");        } else {this.phone = phone;        }    }}

4.3. @NoArgsConstructor

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

@NoArgsConstructor(force = true)public class User {private Long id;
@NonNullprivate String phone;
private final Integer age;}// 編譯后的代碼public class User {private Long id;@NonNullprivate String phone;private final Integer age = null;public User() {    }}

4.4、@Data

作用:@Data 包含了 @ToString、@EqualsAndHashCode、@Getter / @Setter和@RequiredArgsConstructor的功能。

@Datapublic class User {private Long id;private String phone;private Integer status;}// 編譯后的代碼public class User {private Long id;private String phone;private Integer status;
public User() {    }
public Long getId() {return this.id;    }
public String getPhone() {return this.phone;    }
public Integer getStatus() {return this.status;    }
public void setId(Long id) {this.id = id;    }
public void setPhone(String phone) {this.phone = phone;    }
public void setStatus(Integer status) {this.status = status;    }
public boolean equals(Object o) {if(o == this) {return true;        } else if(!(o instanceof User)) {return false;        } else {            User other = (User)o;if(!other.canEqual(this)) {return false;            } else {                label47: {Long this$id = this.getId();Long other$id = other.getId();if(this$id == null) {if(other$id == null) {break label47;                        }                    } else if(this$id.equals(other$id)) {break label47;                    }
return false;                }String this$phone = this.getPhone();                String other$phone = other.getPhone();if(this$phone == null) {if(other$phone != null) {return false;                    }                } else if(!this$phone.equals(other$phone)) {return false;                }Integer this$status = this.getStatus();                Integer other$status = other.getStatus();if(this$status == null) {if(other$status != null) {return false;                    }                } else if(!this$status.equals(other$status)) {return false;                }
return true;            }        }    }
protected boolean canEqual(Object other) {return other instanceof User;    }
public int hashCode() {        boolean PRIME = true;        byte result = 1;Long $id = this.getId();        int result1 = result * 59 + ($id == null?43:$id.hashCode());        String $phone = this.getPhone();        result1 = result1 * 59 + ($phone == null?43:$phone.hashCode());        Integer $status = this.getStatus();        result1 = result1 * 59 + ($status == null?43:$status.hashCode());return result1;    }
public String toString() {return "User(id=" + this.getId() + ", phone=" + this.getPhone() + ", status=" + this.getStatus() + ")";    }}

4.5、@Log

作用:生成log對象,用于記錄日志,可以通過topic屬性來設置getLogger(String name)方法的參數 例如 @Log4j(topic = “com.xxx.entity.User”),默認是類的全限定名,即 類名.class,log支持以下幾種主流日志:

  • @Log java.util.logging.Logger

  • @Log4j org.apache.log4j.Logger

  • @Log4j2 org.apache.logging.log4j.Logger

  • @Slf4j org.slf4j.Logger

  • @XSlf4j org.slf4j.ext.XLogger

  • @CommonsLog org.apache.commons.logging.Log

  • @JBossLog org.jboss.logging.Logger

@Logprivate static final java.util.logging.Logger log =java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4jprivate static final Logger log = org.apache.log4j.Logger.Logger.getLogger(UserService.class);
@Log4j2private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4jprivate static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4jprivate static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);@CommonsLogprivate static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@JBossLogprivate static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Logpublic class UserService {    public void addUser(){        log.info("add user");    }}import java.util.logging.Logger;public class UserService {    private static final Logger log =     Logger.getLogger(UserService.class.getName());    public UserService() {    }}

5、Lombok的優缺點

優點:可以大大減少代碼量,使代碼看起來非常簡潔,大大提高的代碼編寫的效率。

缺點:

  • 需要添加IDEA插件、侵入性很高這個對同事不友好。

  • 代碼量減少了,本質上是缺失代碼的表現。

  • 調試起來會比較麻煩,如果想知道某個類的某個屬性get方法被哪些類調用非常麻煩。

  • 不利于版本升級

  • 雖然省去了手動創建getter/setter方法的麻煩,但大大降低了源代碼的可讀性和完整性,降低了閱讀源代碼的舒適度

6、總結

Lombok可以幫我們提高寫代碼的效率,使代碼看起來更簡潔,它也有不少的缺點不利于后續的運維等等。大家要根據項目的實際情況酌情考慮是否值得使用Lombok。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/270966.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/270966.shtml
英文地址,請注明出處:http://en.pswp.cn/news/270966.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

bzoj2058: [Usaco2010 Nov]Cow Photographs(逆序對)

題目大意&#xff1a;給出n個數的序列&#xff0c;每次可以交換相鄰的兩個數&#xff0c;問把序列變成編號i在編號i1左邊&#xff0c;編號1在編號n右邊(一個環)最少需要多少步。如&#xff1a;35421最少交換兩次變為34512。 一開始看到這題&#xff0c;只會O(n)&#xff0c;后來…

sap實施和開發哪個前景_2021年了!還不知道 SAP顧問的職業前景?

一、先說什么是SAP。百度詞條的解釋&#xff1a;SAP有兩個意思一為“System Applications and Products”的簡稱&#xff0c;是SAP公司的產品——企業管理解決方案的軟件名稱。也代指SAP公司。二為SAP開發的ERP&#xff08;Enterprise-wide Resource Planning&#xff09;軟件名…

Linux找最大最小值的命令,Linux中awk命令正確的求最大值、最小值、平均值、總和...

test.txt文件內容&#xff1a;911352142118求最大值&#xff1a;awk BEGIN {max 0} {if ($10 > max0) max$1} END {print "Max", max} test.txtMax 118求最小值&#xff1a;awk BEGIN {min 65536} {if ($10 < min0) min$1} END {print "Min", min}…

?分布式數據庫技術基礎:數據分布介紹

1、數據分布的定義數據分布是指在分布式環境中通過合理分布數據&#xff0c;提高數據操作自然并行度&#xff0c;以達到最優的執行效率的目的。在構建分布式數據庫系統運行環境時&#xff0c;必須考慮數據如何分布在系統的各個場地上。數據分布主要關注的問題是在分布式數據中&…

uname命令 linux,Linux uname命令詳解

Linux uname命令用于顯示系統信息。uname可顯示電腦以及操作系統的相關信息。語法參數&#xff1a;uname [參數]參數&#xff1a;-a或--all&#xff1a;顯示全部的信息&#xff1b;-m或--machine&#xff1a;顯示電腦類型&#xff1b;-n或-nodename&#xff1a;顯示在網絡上的主…

ios開發text kit_IOS開發入門之TextKit詳解

本文將帶你了解IOS開發入門iOS 開發 富文本詳解之TextKit詳解&#xff0c;希望本文對大家學IOS有所幫助。textkit結構textkit使用步驟#Mark - 1. 自定義label --class CZLabel: UILabel---四個屬性//1.屬性文本存儲private lazy var textStorage NSTextStorage()//2.負責文本…

分布式數據庫技術基礎:數據分片介紹

1、數據分片定義數據分片也成為數據分割&#xff0c;是分布式數據庫的特征之一。一般在一個分布式數據庫中&#xff0c;全局數據庫是由各個局部數據庫邏輯組合而成的&#xff0c;反之各個局部數據庫是由全局數據庫的某種分割邏輯而得的。數據分片得到的各部分元組成為該關系的邏…

9.02

1.input標簽&#xff1a;<input> 標簽用于搜集用戶信息。根據不同的 type 屬性值&#xff0c;輸入字段擁有很多種形式。 輸入字段可以是文本字段、復選框、掩碼后的文本控件、單選按鈕、按鈕等等。例如&#xff1a;Frist name:<input type"text" name"…

分布式數據庫技術基礎:分布透明性相關知識

1、分布透明性介紹數據分布獨立性&#xff1a;主要是指用戶或用戶程序使用分布式數據庫如同使用集中式數據庫那樣&#xff0c;不必關系全局數據的分布情況。也就是說全局數據的邏輯分片、片段的物理位置分配、各場地數據庫的數據模型等情況對用戶和用戶應用程序是透明的。因此分…

宏基4750網卡驅動linux,宏基4750g網卡驅動下載

宏基4750g網卡驅動是宏基筆記本上網驅動&#xff0c;驅動可以幫助用戶體驗便捷上網功能&#xff0c;只需要的雙擊驅動安裝就可以完成&#xff0c;網卡驅動是筆記本必備程序&#xff0c;歡迎用戶來當易網下載體驗&#xff01;驅動介紹Acer宏碁Aspire 4750G筆記本網卡驅動14.4.0.…

python request post 數組_[pve][python]用python3獲取pve狀態信息

手頭的Proxmox VE集群和節點越來越多&#xff0c;需要考慮統一管理了&#xff0c;先定一個小目標——集中狀態監控。以前寫過檢測ceph并用釘釘報警的bash腳本&#xff0c;這次換上洋氣的方式&#xff0c;用python來通過pve的api獲取其狀態信息。首先參考proxmox官方的api(實際上…

分布式數據庫管理系統介紹

1、分布式數據庫管理系統分類綜合型體系結構&#xff1a;主要是指在分布式數據庫建立之前&#xff0c;還沒有建立獨立的集中式數據庫管理系統&#xff0c;設計人員根據用戶的需求&#xff0c;設計出一個全新的完整的數據庫管理系統。聯合型體系結構&#xff1a;主要是指每個節點…

linux中國用戶,Linux中國 適合新用戶的Linux

這個爭論無疑給許多Linux用戶帶來了麻煩。爭論的焦點一般不是哪個發行版是真正最適合新用戶的&#xff0c;而是哪個發行版受這些爭論者的喜愛。如果我們撇開個人喜愛&#xff0c;我們會看到更清楚的一面。但即使這樣&#xff0c;明確的結論也會受到被新用戶的需求和期望的影響。…

關于局部變量表slot的理解

看下圖代碼例子&#xff0c;double類型的b,占用兩個slot,所以index為3和4

Spring LDAP

LDAP Spring LDAP 使用 - Sayi像秋天一樣優雅 - 開源中國社區 http://docs.spring.io/spring-ldap/docs/current/reference/#introduction http://blog.csdn.net/techchan/article/details/5438047轉載于:https://www.cnblogs.com/hello-yz/p/5844784.html

掛起某線程命令 Linux,linux 線程掛起恢復的簡單示例

參考&#xff1a;寫了個demo&#xff1a;#include #include static pthread_mutex_t mutex;static pthread_cond_t cond;static int flag 0;void srpthread_init(){pthread_mutex_init(&mutex,NULL);pthread_cond_init(&cond,NULL);}void srpthread_suspend(){pthread…

分布式查詢處理和優化相關知識介紹

一、分布式數據庫查詢考慮的因素1、和集中式數據查詢一樣需要考慮查詢語言語句的優化2、數據和信息均需要通過通信線路進行數據傳輸&#xff0c;存在傳輸延遲問題從而影響整個查詢的執行效率。3、網絡中多處理器的存在提供了并行數據處理和傳輸的機會&#xff0c;可以充分利用該…