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。