簡介
lombok
是一個編譯級別的插件,它可以在項目編譯的時候生成一些代碼。比如日常開發過程中需要生產大量的javabean
文件,每個javabean
都需要提供大量的get
和set
方法,如果字段較多且發生變動的話修改起來相對繁瑣,相應的lombok
可以通過注解(@getter,@setter)
為我們省去手動創建getter
和setter
方法的麻煩,它能夠在我們編譯源碼的時候自動幫我們生成getter
和setter
方法。
即它最終能夠達到的效果是:在源碼中沒有getter
和setter
方法,但是在編譯生成的字節碼文件中有getter
和setter
方法。
另外在項目開發階段,一個class
的屬性是一直變化的,今天可能增加一個字段,明天可能刪除一個字段。每次變化都需要修改對應的模板代碼。另外,有的class
的字段超級多,多到一眼看不完。如果加上模板代碼,更難一眼看出來。更有甚者,由于字段太多,想要使用builder
來創建。手動創建builder
和字段和原來的類夾雜在一起,看起來很凌亂。lombok
的@builder
即可解決這個問題。
官網地址:https://projectlombok.org/
lombok最新版本號:http://jcenter.bintray.com/org/projectlombok/lombok/
官方文檔: https://projectlombok.org/features/all
lombok 注解介紹:http://www.ythuaji.com.cn/article/157124.html
安裝 lombok 插件
添加注解支持
添加依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
buildscript { ext { springbootversion = '2.0.2.release' } repositories { mavencentral() } dependencies { classpath( "org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}" ) } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.yanjun' version = '0.0.1-snapshot' sourcecompatibility = 1.8 repositories { mavencentral() } dependencies { compile( 'org.springframework.boot:spring-boot-starter' , 'org.springframework.boot:spring-boot-starter-web' , ) //添加lombok依賴 compile 'org.projectlombok:lombok:1.18.0' } |
常用注解
- @data 注解在類上;提供類所有屬性的 getting 和 setting 方法,此外還提供了equals、canequal、hashcode、tostring 方法
- @setter :注解在屬性上;為屬性提供 setting 方法
- @setter :注解在屬性上;為屬性提供 getting 方法
- @log4j :注解在類上;為類提供一個 屬性名為log 的 log4j 日志對象
- @noargsconstructor :注解在類上;為類提供一個無參的構造方法
- @allargsconstructor :注解在類上;為類提供一個全參的構造方法
- @cleanup : 可以關閉流
- @builder : 被注解的類加個構造者模式
- @synchronized : 加個同步鎖
- @sneakythrows : 等同于try/catch 捕獲異常
- @nonnull : 如果給參數加個這個注解 參數為null會拋出空指針異常
- @value : 注解和@data類似,區別在于它會把所有成員變量默認定義為private final修飾,并且不會生成set方法。
@nonnull 判空處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.yanjun.mybatis.bean; import lombok.data; import lombok.noargsconstructor; import lombok.nonnull; @data @noargsconstructor public class user { public integer age; @nonnull public string name; } |
相當于
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.yanjun.mybatis.bean; import lombok.nonnull; public class user { public integer age; @nonnull public string name; public integer getage() { return this .age; } @nonnull public string getname() { return this .name; } public void setage(integer age) { this .age = age; } public void setname( @nonnull string name) { if (name == null ) { throw new nullpointerexception( "name is marked @nonnull but is null" ); } else { this .name = name; } } } |
測試
1
2
|
user user = new user(); user.setname( null ); |
效果
exception in thread "main" java.lang.nullpointerexception: name is marked @nonnull but is null
at com.yanjun.mybatis.bean.user.setname(user.java:7)
at com.yanjun.mybatis.lombokapplication.main(lombokapplication.java:15)
@data 提供 get、set 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.yanjun.mybatis.bean; import lombok.data; @data //自動生成 get、set 方法 public class user { integer id; string name; integer age; public static void main(string[] args) { //測試方法 user user = new user(); user.setname( "zhaoyanjun" ); user.setage( 20 ); user.setid( 1 ); } } |
@slf4j 日志打印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.yanjun.mybatis.bean; import lombok.data; import lombok.extern.slf4j.slf4j; @data //自動生成 get、set 方法 @slf4j //日志打印 public class user { integer id; string name; integer age; public static void main(string[] args) { user user = new user(); user.setname( "zhaoyanjun" ); user.setage( 20 ); user.setid( 1 ); log.info( "日志" + user.tostring()); } } |
@allargsconstructor 全參數構造函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.yanjun.mybatis.bean; import lombok.allargsconstructor; import lombok.data; import lombok.extern.slf4j.slf4j; @data //自動生成 get、set 方法 @allargsconstructor //自動生成全參數構造函數 @slf4j //日志打印 public class user { integer id; string name; integer age; public static void main(string[] args) { user user = new user( 1 , "zhaoyanjun" , 20 ); log.info( "日志" + user.tostring()); } } |
@tostring 自動生成tostring方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.yanjun.mybatis.bean; import lombok.allargsconstructor; import lombok.data; import lombok.tostring; import lombok.extern.slf4j.slf4j; @data //自動生成 get、set 方法 @allargsconstructor //自動生成全參數構造函數 @slf4j //日志打印 @tostring (of = { "id" , "age" }) //tostring() 方法里面只打印 id ,name 字段 public class user { integer id; string name; integer age; public static void main(string[] args) { user user = new user( 1 , "zhaoyanjun" , 20 ); log.info( "日志" + user.tostring()); } } |
tostring() 方法排除字段
1
2
|
//排除字段 @tostring (exclude = { "name" }) //tostring() 方法中,name 字段不參與打印 |
@value 用于注解final類
@value
注解和@data
類似,區別在于它會把所有成員變量默認定義為 private final
修飾,并且不會生成set
方法。
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.yanjun.mybatis.bean; import lombok.value; @value public class user { integer id = 1 ; string name = "zhaoyanjun" ; integer age = 3 ; } |
編譯后的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public final class user { private final integer id = 1 ; private final string name = "zhaoyanjun" ; private final integer age = 3 ; public user() { } public integer getid() { return this .id; } public string getname() { this .getclass(); return "zhaoyanjun" ; } public integer getage() { return this .age; } //省略部分代碼 ...... } |
@builder : 被注解的類加個構造者模式
1
2
3
4
5
6
7
8
9
10
11
|
import lombok.builder; @builder public class user { public integer id; public string name; public integer age; } |
相當于以下代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.yanjun.mybatis.bean; public class user { public integer id; public string name; public integer age; user(integer id, string name, integer age) { this .id = id; this .name = name; this .age = age; } public static user.userbuilder builder() { return new user.userbuilder(); } public static class userbuilder { private integer id; private string name; private integer age; userbuilder() { } public user.userbuilder id(integer id) { this .id = id; return this ; } public user.userbuilder name(string name) { this .name = name; return this ; } public user.userbuilder age(integer age) { this .age = age; return this ; } public user build() { return new user( this .id, this .name, this .age); } public string tostring() { return "user.userbuilder(id=" + this .id + ", name=" + this .name + ", age=" + this .age + ")" ; } } } |
使用
1
2
3
4
5
6
7
8
|
userbuilder userbuilder = user.builder(); user user = userbuilder .age( 10 ) .id( 1 ) .name( "yanjun" ) .build(); system.out.println( ": " + userbuilder.tostring()); |
@synchronized : 加個同步鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.yanjun.mybatis.bean; import lombok. synchronized ; public class user { public integer age; public string name; //普通方法,相當于對象鎖 @synchronized int run1() { return 1 ; } //靜態方法,相當于類鎖 @synchronized static int run2() { return 2 ; } } |
代碼的效果相當于
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class user { private final object $lock = new object[ 0 ]; private static final object $lock = new object[ 0 ]; public integer age; public string name; public user() { } int run1() { object var1 = this .$lock; synchronized ( this .$lock) { return 1 ; } } static int run2() { object var0 = $lock; synchronized ($lock) { return 2 ; } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/zhaoyanjun6/article/details/80743302