摘要: Lombok 通過提供簡單的語法注解形式來幫助簡化消除一些必須有但顯得很臃腫的 java 代碼。典型的是對于 POJO對象的簡化(如自動幫我們生成Setter和Getter等),有了Lombok的加持,開發(fā)人員可以免去很多重復且臃腫的操作,**極大地提高java代碼的信噪比**,因此我們必須嘗試并應用起來
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_1_2018041311113666)
Lombok 通過提供簡單的語法注解形式來幫助簡化消除一些必須有但顯得很臃腫的 java 代碼。典型的是對于 POJO對象的簡化(如自動幫我們生成Setter和Getter等),有了Lombok的加持,開發(fā)人員可以免去很多重復且臃腫的操作,極大地提高java代碼的信噪比,因此我們必須嘗試并應用起來!
方法一:直接在IDEA界面中配置
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_2_20180413111136222)
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_3_20180413111136378)
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_4_20180413111136550)
上述安裝完成以后需要重啟IDEA生效!
方法二:手動下載Lombok插件安裝
有時由于網(wǎng)絡原因,上面方法一這種方式安裝失敗,因此只能手動下載安裝
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_5_20180413111136691)
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_6_20180413111136831)
IDE中設置完成以后需要在pom.xml中添加如下所示的lombok依賴才能使用
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_7_20180413111136972)
@Getter and @Setter / 自動為屬性提供 Set和Get 方法 @ToString / 該注解的作用是為類自動生成toString()方法 @EqualsAndHashCode / 為對象字段自動生成hashCode和equals實現(xiàn) @AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor / 顧名思義,為類自動生成對應參數(shù)的constructor @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog / 自動為類添加對應的log支持 @Data / 自動為所有字段添加@ToString, @EqualsAndHashCode, @Getter,為非final字段添加@Setter,和@RequiredArgsConstructor,本質(zhì)上相當于幾個注解的綜合效果 @NonNull / 自動幫助我們避免空指針。作用在方法參數(shù)上的注解,用于自動生成空值參數(shù)檢查 @Cleanup / 自動幫我們調(diào)用close()方法。作用在局部變量上,在作用域結(jié)束時會自動調(diào)用close方法釋放資源
下文就Lombok中用的最為頻繁的@Data和@Log注解進行代碼實戰(zhàn)!
官網(wǎng)關(guān)于@Data注解的解釋如下:
All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor!
不難理解,其可以看成是多個Lombok注解的集成,因此使用很方便!
public class UserLombok { private final String name; private int age; private double score; private String[] tags; public UserLombok(String name) { this.name = name; } public String getName() { return this.name; } void setAge(int age) { this.age = age; } public int getAge() { return this.age; } public void setScore(double score) { this.score = score; } public double getScore() { return this.score; } public String[] getTags() { return this.tags; } public void setTags(String[] tags) { this.tags = tags; } @Override public String toString() { return 'DataExample(' this.getName() ', ' this.getAge() ', ' this.getScore() ', ' Arrays.deepToString(this.getTags()) “)”; } protected boolean canEqual(Object other) { return other instanceof DataExample; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof DataExample)) return false; DataExample other = (DataExample) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false; if (this.getAge() != other.getAge()) return false; if (Double.compare(this.getScore(), other.getScore()) != 0) return false; if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final long temp1 = Double.doubleToLongBits(this.getScore()); result = (result*PRIME) (this.getName() == null ? 43 : this.getName().hashCode()); result = (result*PRIME) this.getAge(); result = (result*PRIME) (int)(temp1 ^ (temp1 >>> 32)); result = (result*PRIME) Arrays.deepHashCode(this.getTags()); return result; } }
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_8_2018041311113781)
在IDEA中使用時,Lombok的注解會自動補全,如下圖所示:
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_9_20180413111137128)
![](http://image109.360doc.com/DownloadImg/2018/04/1311/130073274_10_20180413111137191)
由下圖我們可以看到IDEA依然可以自動為我們補全由Lombok自動生成的代碼:
![](http://pubimage.360doc.com/wz/default.gif)
由于Lombok為我們自動生成了toString方法,因此對象的打印結(jié)果如下:
![](http://pubimage.360doc.com/wz/default.gif)
在我的文章 Spring Boot日志框架實踐 一文中,我們使用Log4j2來作為日志對象,其寫法如下:
![](http://pubimage.360doc.com/wz/default.gif)
若改用Lombok后,寫法變得更加簡潔,我們只需要引入對應的@Log注解即可完成log對象的生成:
![](http://pubimage.360doc.com/wz/default.gif)
怎么樣,是不是一切都是那么地優(yōu)雅!
活動福利
4月12日-4月15日,參與我們的問卷調(diào)查【“提需求”贏福利,程序員的需求是什么樣的?】,并在文末留言您對開源中國公眾號的意見或建議。
我們將在活動結(jié)束后,抽選數(shù)名幸運用戶送出精美技術(shù)圖書一本(圖片僅供參考,書籍隨機贈送)
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
|