本文介紹了springboot優(yōu)雅編碼之lombok加持,分享給大家,具體如下:
概述
lombok 通過提供簡單的語法注解形式來幫助簡化消除一些必須有但顯得很臃腫的 java 代碼。典型的是對于 pojo對象的簡化(如自動(dòng)幫我們生成setter和getter等),有了lombok的加持,開發(fā)人員可以免去很多重復(fù)且臃腫的操作,極大地提高java代碼的信噪比,因此我們必須嘗試并應(yīng)用起來!
intellij idea上配置
方法一:直接在idea界面中配置
首先進(jìn)入plugins界面:
然后搜索并安裝lombok插件:
最后不要忘了開啟annotation processors的enable選項(xiàng):
上述安裝完成以后需要重啟idea生效!
方法二:手動(dòng)下載lombok插件安裝
有時(shí)由于網(wǎng)絡(luò)原因,上面方法一這種方式安裝失敗,因此只能手動(dòng)下載安裝
下載lombok插件:
https://github.com/mplushnikov/lombok-intellij-plugin/releases
plugins -> install plugin from disk... 選擇下載的zip包安裝
重啟idea即可
ide中設(shè)置完成以后需要在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
/ 自動(dòng)為屬性提供 set和get 方法 -
@tostring
/ 該注解的作用是為類自動(dòng)生成tostring()方法 -
@equalsandhashcode
/ 為對象字段自動(dòng)生成hashcode和equals實(shí)現(xiàn) -
@allargsconstructor, @requiredargsconstructor and @noargsconstructor
/ 顧名思義,為類自動(dòng)生成對應(yīng)參數(shù)的constructor -
@log, @log4j, @log4j2, @slf4j, @xslf4j, @commonslog, @jbosslog
/ 自動(dòng)為類添加對應(yīng)的log支持 -
@data
/ 自動(dòng)為所有字段添加@tostring, @equalsandhashcode, @getter,為非final字段添加@setter,和@requiredargsconstructor,本質(zhì)上相當(dāng)于幾個(gè)注解的綜合效果 -
@nonnull
/ 自動(dòng)幫助我們避免空指針。作用在方法參數(shù)上的注解,用于自動(dòng)生成空值參數(shù)檢查 -
@cleanup
/ 自動(dòng)幫我們調(diào)用close()方法。作用在局部變量上,在作用域結(jié)束時(shí)會(huì)自動(dòng)調(diào)用close方法釋放資源
下文就lombok中用的最為頻繁的@data
和@log
注解進(jìn)行代碼實(shí)戰(zhàn)!
@data注解使用
官網(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!
不難理解,其可以看成是多個(gè)lombok注解的集成,因此使用很方便!
先來創(chuàng)建一個(gè)pojo實(shí)體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中使用時(shí),lombok的注解會(huì)自動(dòng)補(bǔ)全,如下圖所示:
我們來寫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依然可以自動(dòng)為我們補(bǔ)全由lombok自動(dòng)生成的代碼:
結(jié)果打印
由于lombok為我們自動(dòng)生成了tostring方法,因此對象的打印結(jié)果如下:
1
|
userlombok(name=hansonwang99, age= 18 , score= 99.0 , tags=[apple, juice]) |
@log注解實(shí)戰(zhàn)
在我的文章 spring boot日志框架實(shí)踐 一文中,我們使用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后,寫法變得更加簡潔,我們只需要引入對應(yīng)的@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”; } } |
怎么樣,是不是一切都是那么地優(yōu)雅!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000014247338