Java:Lombok插件用法笔记
1、Lombok是什么東東?
官方介紹Lombok項(xiàng)目是一個(gè)Java庫(kù),它可以自動(dòng)嵌入你的編輯器和構(gòu)建工具中,從而減少你的代碼量。永遠(yuǎn)不要再寫(xiě)另一個(gè)getter或equals方法,它帶有一個(gè)注釋的你的類(lèi)有一個(gè)功能全面的生成器,自動(dòng)化你的日志記錄變量等等功能。簡(jiǎn)單來(lái)說(shuō)就是使用Lombok,通過(guò)注解,讓你不再需要編寫(xiě)getter、equals等屬性方法,減少樣板代碼的編寫(xiě)、起到提升代碼效率的功能。
2、IDEA如何安裝Lombok
IDEA開(kāi)發(fā)工具如果需要正常使用Lombok,就需要安裝Lombok插件,這樣IDEA就可以正常識(shí)別Lombok注解,從而可以正常編譯項(xiàng)目。今天給大家介紹一下如何通過(guò)IDEA安裝IDEA插件。
安裝方法:
1、工具欄點(diǎn)擊File→Settings設(shè)置界面→找到Plugins→找到Lombok插件然后點(diǎn)擊install→重啟IDEA,安裝Lombok插件如下圖:
2、點(diǎn)擊File-- Settings設(shè)置界面,開(kāi)啟 AnnocationProcessors如下圖:
開(kāi)啟 AnnocationProcessors目的是讓Lombok注解在編譯階段起作用。
3、如何使用Lombok
3.1 添加依賴(lài)
<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; }}說(shuō)明:@Getter @Setter 注解在類(lèi)上,表示為類(lèi)中的所有字段生成Getter&Setter方法。也可以使用@Data來(lái)代替@Getter @Setter,但是@Data會(huì)引入更多的注解。
4.2 @NonNull
作用:給字段賦值時(shí)(調(diào)用字段的setter方法時(shí)),如果傳遞的參數(shù)值為null,則會(huì)拋出空異常NullPointerException,生成setter方法時(shí)會(huì)對(duì)參數(shù)是否為空進(jìn)行檢查。
@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
作用:生成一個(gè)無(wú)參構(gòu)造方法。當(dāng)類(lèi)中有final字段沒(méi)有被初始化時(shí),編譯器就會(huì)報(bào)錯(cuò),這個(gè)時(shí)候可用@NoArgsConstructor(force = true),然后為沒(méi)有初始化的final字段設(shè)置默認(rèn)值 0 / false / null, 這樣編譯器就不會(huì)報(bào)錯(cuò)。對(duì)于具有約束的字段(例如@NonNull字段),不會(huì)生成檢查或分配,因此請(qǐng)?zhí)貏e注意,正確初始化這些字段之前,這些約束是無(wú)效的。
@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對(duì)象,用于記錄日志,可以通過(guò)topic屬性來(lái)設(shè)置getLogger(String name)方法的參數(shù) 例如 @Log4j(topic = “com.xxx.entity.User”),默認(rèn)是類(lèi)的全限定名,即 類(lèi)名.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
5、Lombok的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):可以大大減少代碼量,使代碼看起來(lái)非常簡(jiǎn)潔,大大提高的代碼編寫(xiě)的效率。
缺點(diǎn):
需要添加IDEA插件、侵入性很高這個(gè)對(duì)同事不友好。
代碼量減少了,本質(zhì)上是缺失代碼的表現(xiàn)。
調(diào)試起來(lái)會(huì)比較麻煩,如果想知道某個(gè)類(lèi)的某個(gè)屬性get方法被哪些類(lèi)調(diào)用非常麻煩。
不利于版本升級(jí)
雖然省去了手動(dòng)創(chuàng)建getter/setter方法的麻煩,但大大降低了源代碼的可讀性和完整性,降低了閱讀源代碼的舒適度
6、總結(jié)
Lombok可以幫我們提高寫(xiě)代碼的效率,使代碼看起來(lái)更簡(jiǎn)潔,它也有不少的缺點(diǎn)不利于后續(xù)的運(yùn)維等等。大家要根據(jù)項(xiàng)目的實(shí)際情況酌情考慮是否值得使用Lombok。
總結(jié)
以上是生活随笔為你收集整理的Java:Lombok插件用法笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 9.5题解
- 下一篇: java美元兑换,(Java实现) 美元