作者:Mr.ml
https://blog.csdn.net/MarcoAsensio/
前言
what?你的 Java 代碼中還充斥著大量的 set/get 方法?
我們在剛開始學習 Java 語言的時候講過,面向對象的三大特徵就是封裝,繼承,和多態。在 Java 中,要保證封裝性,需要將成員變量私有化,對外提供 set/get 方法來訪問,雖然現在的 IDE,像 eclipse,IDEA都提供了快捷鍵,來生成 set/get 方法。
但是在做項目的時候,一個 JavaBean 往往會有很多的成員變量,一個變量對應兩個方法,如果有10幾個成員變量,那麼會對應20多個方法,也許還要去寫構造器、equals 等方法,而且需要維護。這樣一來,會使代碼變得非常冗餘,這些顯得很冗長也沒有太多技術含量,一旦修改屬性,就容易出現忘記修改對應方法的失誤。
我在看大佬的項目的源碼的時候,看到他們的代碼中都沒有 set/get 方法,取而代之的是在 JavaBean 上標註的註解,我感到非常的好奇,原來他們是用了一種叫做 Lombok 的插件,便去詳細瞭解了這個插件。
注:Lombok相關擴展:
Java開發神器:Lombok 學習指南
加倍提升開發效率,繼續深挖一下Lombok的使用
極簡代碼神器:Lombok使用教程
Lombok 背景介紹
官方介紹如下:
Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code.
大致意思是 Lombok 通過增加一些“處理程序”,可以讓 Java 變得簡潔、快速。
Lombok 使用方法
Lombok 能通過註解的方式,在編譯時自動為屬性生成構造器、getter/setter、equals、hashcode、toString 方法。出現的神奇就是在源碼中沒有 getter 和 setter 方法,但是在編譯生成的字節碼文件中有 getter 和 setter 方法。這樣就省去了手動重建這些代碼的麻煩,使代碼看起來更簡潔些。
Lombok 的使用跟引用 jar 包一樣,可以在官網(https://projectlombok.org/download)下載 jar 包,也可以使用 maven 添加依賴:
<code><
dependency
><
groupId
>org.projectlombokgroupId
><
artifactId
>lombokartifactId
><
version
>1.18.10version
><
scope
>providedscope
>dependency
>/<code>
注意:第一次使用 Lombok 插件需要做如下幾步配置
1.將 Lombok 插件安裝到 IDEA
file -> setting
選中 Plugins,搜索 Lombok,點擊 Install
2.選擇默認的編譯方式為 javac,因為 eclipse 是不支持 Lombok 的編譯方式的,javac 支持 Lombok 的編譯方式。
3.打開註解生成器 Enable annotation processing
再次注意:
IntelliJ IDEA 2019.2(也就是我用的版本)默認是不支持 Lombok 插件的,需要去
https://plugins.jetbrains.com/plugin/6317-lombok/versions
下載對應版本的插件,然後手動引入,在 IDEA 中選擇 File -> Setting -> plugins 找到 Install Plugin from Disk…(注意版本不同位置可能有所差異)
接下來我們來分析 Lombok 中註解的具體用法
@Data
@Data 註解在類上,會為類的所有屬性自動生成 setter/getter、equals、canEqual、hashCode、toString 方法,如為 final 屬性,則不會為該屬性生成 setter 方法。
比如我們寫一個學生類
<code>public
class
Student {private
String
name;private
Integer age;private
Integer id;private
String
major; }/<code>
這樣就可以調用 set/get 方法了。
@Getter/@Setter
如果覺得@Data 太過殘暴(因為@Data 集合了@ToString、@EqualsAndHashCode、@Getter/@Setter、@RequiredArgsConstructor 的所有特性)不夠精細,可以使用@Getter/@Setter 註解,此註解在屬性上,可以為相應的屬性自動生成 set/get 方法。
<code>public
class
Student {private
String
name;private
Integer age;private
Integer id;private
String
major;public
static
void
main(String
[] args) { Student stu =new
Student(); stu.setName("Mr.ml"
); } }/<code>
@NonNull
該註解用在屬性或構造器上,Lombok 會生成一個非空的聲明,可用於校驗參數,能幫助避免空指針。
<code>public
class
Student {private
String
name;private
Integer age;private
Integer id;private
String
major;public
Student(String
name) {this
.name = name; } }/<code>
@Cleanup
該註解能幫助我們自動調用 close() 方法,很大的簡化了代碼。
<code>public
class
CleanupExample
{public
static
void
main
(String[] args)
throws
IOException { InputStream in =new
FileInputStream(args[0
]); OutputStream out =new
FileOutputStream(args[1
]);byte
[] b =new
byte
[10000
];while
(true
) {int
r = in.read(b);if
(r == -1
)break
; out.write(b,0
, r); } } }/<code>
@EqualsAndHashCode
默認情況下,會使用所有非靜態(non-static)和非瞬態(non-transient)屬性來生成 equals 和 hashCode,也能通過 exclude 註解來排除一些屬性。
<code> (exclude={"id"
,"shape"
})public
class
EqualsAndHashCodeExample
{private
transient
int
transientVar =10
;private
String name;private
double
score;private
Shape shape =new
Square(5
,10
);private
String[] tags;private
int
id;public
StringgetName
()
{return
this
.name; } (callSuper=true
)public
static
class
Square
extends
Shape
{private
final
int
width, height;public
Square
(
int
width,int
height) {this
.width = width;this
.height = height; } } }/<code>
@ToString
類使用@ToString 註解,Lombok 會生成一個 toString() 方法,默認情況下,會輸出類名、所有屬性(會按照屬性定義順序),用逗號來分割。
通過將 includeFieldNames 參數設為 true,就能明確的輸出 toString() 屬性。這一點是不是有點繞口,通過代碼來看會更清晰些。
<code> (exclude="id"
)public
class
ToStringExample
{private
static
final
int
STATIC_VAR =10
;private
String name;private
Shape shape =new
Square(5
,10
);private
String[] tags;private
int
id;public
StringgetName
()
{return
this
.getName(); } (callSuper=true
, includeFieldNames=true
)public
static
class
Square
extends
Shape
{private
final
int
width, height;public
Square
(
int
width,int
height) {this
.width = width;this
.height = height; } } } /<code>
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
無參構造器、部分參數構造器、全參構造器。Lombok沒法實現多種參數構造器的重載。
<code> (staticName ="of"
) (access = AccessLevel.PROTECTED)public
class
ConstructorExample
<T
> {private
int
x, y;private
T description;public
static
class
NoArgsExample
{private
String field; } }/<code>
對了,在這裡說一下,我目前是在職Java開發,如果你現在正在學習Java,瞭解Java,渴望成為一名合格的Java開發工程師,在入門學習Java的過程當中缺乏基礎入門的視頻教程,可以關注並私信我:01。獲取。我這裡有最新的Java基礎全套視頻教程。