首頁>技術>

1、Lombok是什麼東東?

官方介紹Lombok專案是一個Java庫,它可以自動嵌入你的編輯器和構建工具中,從而減少你的程式碼量。永遠不要再寫另一個getter或equals方法,它帶有一個註釋的你的類有一個功能全面的生成器,自動化你的日誌記錄變數等等功能。

簡單來說就是使用Lombok,透過註解,讓你不再需要編寫getter、equals等屬性方法,減少樣板程式碼的編寫、起到提升程式碼效率的功能。

2、IDEA如何安裝Lombok

IDEA開發工具如果需要正常使用Lombok,就需要安裝Lombok外掛,這樣IDEA就可以正常識別Lombok註解,從而可以正常編譯專案。今天給大家介紹一下如何透過IDEA安裝IDEA外掛。

安裝方法:

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

3、如何使用Lombok3.1 新增依賴
<dependency>    <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId>    <optional>true</optional></dependency>
4 、 Lombok常用用法示例

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

4.1 @Getter/@Setter
public class User {    @Getter @Setter    private 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;    @NonNull    private String phone;}//編譯後生成的程式碼public class User {    private Long id;    @NonNull    private String phone;    public User() {    }    public Long getId() {        return this.id;    }	public void setId(Long id) {        this.id = id;    }    @NonNull    public 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;    @NonNull    private String phone;    private final Integer age;}// 編譯後的程式碼public class User {    private Long id;    @NonNull    private 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。

12
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 「ELK」elastalert 日誌告警