本文介紹了springboot優雅編碼之lombok加持,分享給大家,具體如下:
概述
lombok 通過提供簡單的語法注解形式來幫助簡化消除一些必須有但顯得很臃腫的 java 代碼。典型的是對于 pojo對象的簡化(如自動幫我們生成setter和getter等),有了lombok的加持,開發人員可以免去很多重復且臃腫的操作,極大地提高java代碼的信噪比,因此我們必須嘗試并應用起來!
intellij idea上配置
方法一:直接在idea界面中配置
首先進入plugins界面:
然后搜索并安裝lombok插件:
最后不要忘了開啟annotation processors的enable選項:
上述安裝完成以后需要重啟idea生效!
方法二:手動下載lombok插件安裝
有時由于網絡原因,上面方法一這種方式安裝失敗,因此只能手動下載安裝
下載lombok插件:
https://github.com/mplushnikov/lombok-intellij-plugin/releases
plugins -> install plugin from disk... 選擇下載的zip包安裝
重啟idea即可
ide中設置完成以后需要在pom.xml中添加如下所示的lombok依賴才能使用
1
2
3
4
5
|
<dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version> 1.16 . 16 </version> </dependency> |
lombok主要注解
-
@getter and @setter
/ 自動為屬性提供 set和get 方法 -
@tostring
/ 該注解的作用是為類自動生成tostring()方法 -
@equalsandhashcode
/ 為對象字段自動生成hashcode和equals實現 -
@allargsconstructor, @requiredargsconstructor and @noargsconstructor
/ 顧名思義,為類自動生成對應參數的constructor -
@log, @log4j, @log4j2, @slf4j, @xslf4j, @commonslog, @jbosslog
/ 自動為類添加對應的log支持 -
@data
/ 自動為所有字段添加@tostring, @equalsandhashcode, @getter,為非final字段添加@setter,和@requiredargsconstructor,本質上相當于幾個注解的綜合效果 -
@nonnull
/ 自動幫助我們避免空指針。作用在方法參數上的注解,用于自動生成空值參數檢查 -
@cleanup
/ 自動幫我們調用close()方法。作用在局部變量上,在作用域結束時會自動調用close方法釋放資源
下文就lombok中用的最為頻繁的@data
和@log
注解進行代碼實戰!
@data注解使用
官網關于@data注解的解釋如下:
all together now: a shortcut for @tostring, @equalsandhashcode, @getter on all fields, @setter on all non-final fields, and @requiredargsconstructor!
不難理解,其可以看成是多個lombok注解的集成,因此使用很方便!
先來創建一個pojo實體userlombok,普通的寫法如下:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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; } } |
lombok加持后,寫法可簡化為:
1
2
3
4
5
6
7
|
@data public class userlombok { private final string name; private int age; private double score; private string[] tags; } |
在idea中使用時,lombok的注解會自動補全,如下圖所示:
我們來寫pojo的測試代碼
1
2
3
4
5
6
7
8
|
public static void main( string[] args ) { userlombok userlombok = new userlombok("hansonwang99”); userlombok.setage( 18 ); string[] array = new string[]{ "apple" ,"juice”}; userlombok.settags( array ); userlombok.setscore( 99.0 ); system.out.println(userlombok); } |
由下圖我們可以看到idea依然可以自動為我們補全由lombok自動生成的代碼:
結果打印
由于lombok為我們自動生成了tostring方法,因此對象的打印結果如下:
1
|
userlombok(name=hansonwang99, age= 18 , score= 99.0 , tags=[apple, juice]) |
@log注解實戰
在我的文章 spring boot日志框架實踐 一文中,我們使用log4j2來作為日志對象,其寫法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@restcontroller @requestmapping ("/testlogging”) public class loggingtestcontroller { private final logger logger = logmanager.getlogger( this .getclass()); @getmapping ("/hello”) public string hello() { for ( int i= 0 ;i<10_0000;i++){ logger.info("info execute index method”); logger.warn("warn execute index method”); logger.error("error execute index method”); } return "my first springboot application”; } } |
若改用lombok后,寫法變得更加簡潔,我們只需要引入對應的@log注解即可完成log對象的生成:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@restcontroller @requestmapping ("/testloggingwithlombok”) @log4j2 public class loggingtestcontrollerlombok { @getmapping ("/hello”) public string hello() { for ( int i= 0 ;i<10_0000;i++){ log.info("info execute index method”); log.warn("warn execute index method”); log.error("error execute index method”); } return "my first springboot application”; } } |
怎么樣,是不是一切都是那么地優雅!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000014247338