前言
眾所周知spring boot是非常高效的開發框架,lombok是一套代碼模板解決方案,將極大提升開發的效率,這里介紹給大家使用。文中詳細介紹了lombok的安裝與使用教程,話不多說了,來一起看看詳細的介紹吧。
1. lombok
lombok想要解決了的是在我們實體bean中大量的getter/setter方法,以及tostring, hashcode等可能不會用到,但是某些時候仍然需要復寫,以期方便使用的方法;在使用lombok之后,將由其來自動幫你實現代碼生成,注意,其是在運行過程中,幫你自動生成的。就是說,將極大減少你的代碼總量。
lombok的官方地址:https://projectlombok.org/
2. lombok的安裝
在springboot1.4.1項目的pom.xml中新增如下信息:
1
2
3
4
|
<dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> </dependency> |
注意:這里無需指定版本,因為spring boot中已經默認引入了這個類庫,且指定了其scope。 這個即將lombok引入了項目,可以引用其類庫標注。
針對不同的ide,lombok提供了不同的解決方案,筆者使用的是sts,故這介紹一下如何使用lombok插件在sts中:
>> 1. 下載最新的lombok,最新版本。1.16.10 https://projectlombok.org/download.html
>> 2. 切到lombok下載的目錄,運行命令: java -jar lombok.jar
選中目錄之后,就會看到如下窗口:
大家可以看到,這里的會使用javaagent的方式寫入sts的啟動過程中。在確定之后,我們切到sts的安裝目錄,驗證一下sts.ini文件:
大家也可以發現,在sts的目錄下,也有lombok.jar文件的存在,整個安裝過程即可完成。
3. lombok的使用
主要是基于標注來進行信息的封裝和使用:
@nonnull: 標識對象是否為空,為空則拋出異常
@getter: 自動生成getter方法
@setter: 自動生成setter
@tostring: 覆蓋tostring方法
@equalsandhashcode: 覆蓋equal和hashcode方法
@data: @getter/@setter, @tostring, @equalandhashcode等組合
@slf4j: 默認使用slf4j的日志對象
4. 使用示例:
示例getter/setter方法以及日志使用:
1
2
3
4
5
6
7
8
9
|
import lombok.data; import lombok.extern.slf4j.slf4j; @slf4j @data public class testbean { private string name; private int 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
|
import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; import lombok.extern.slf4j.slf4j; @runwith (springrunner. class ) @springboottest @slf4j public class testentitybean { testbean bean = new testbean(); @autowired private mycase mycase; //@test public void test() { bean.setage( 123 ); bean.setname( "zhangsan" ); log.info(bean.tostring()); } } |
用例運行結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v1. 4.1 .release) 2016 - 10 - 24 11 : 49 : 19.521 info 700 --- [ main] org.cuckoo.report.brain.testentitybean : starting testentitybean on 08 - 201412015324 with pid 700 (started by junfengchen in d:\dev\workspace\jspdemo) 2016 - 10 - 24 11 : 49 : 19.523 info 700 --- [ main] org.cuckoo.report.brain.testentitybean : no active profile set, falling back to default profiles: default 2016 - 10 - 24 11 : 49 : 19.647 info 700 --- [ main] o.s.w.c.s.genericwebapplicationcontext : refreshing org.springframework.web.context.support.genericwebapplicationcontext @4f80542f : startup date [mon oct 24 11 : 49 : 19 cst 2016 ]; root of context hierarchy 2016 - 10 - 24 11 : 49 : 22.191 info 700 --- [ main] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice : org.springframework.web.context.support.genericwebapplicationcontext @4f80542f : startup date [mon oct 24 11 : 49 : 19 cst 2016 ]; root of context hierarchy 2016 - 10 - 24 11 : 49 : 22.290 info 700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/foo]}" onto public java.lang.string org.cuckoo.report.brain.welcomecontroller.foo(java.util.map<java.lang.string, java.lang.object>) 2016 - 10 - 24 11 : 49 : 22.293 info 700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/],methods=[get]}" onto public java.lang.string org.cuckoo.report.brain.welcomecontroller.welcome(java.util.map<java.lang.string, java.lang.object>) 2016 - 10 - 24 11 : 49 : 22.295 info 700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest) 2016 - 10 - 24 11 : 49 : 22.296 info 700 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) 2016 - 10 - 24 11 : 49 : 22.351 info 700 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars /**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2016-10-24 11:49:22.351 info 700 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2016-10-24 11:49:22.409 info 700 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/ favicon.ico] onto handler of type [ class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2016 - 10 - 24 11 : 49 : 22.642 info 700 --- [ main] org.cuckoo.report.brain.testentitybean : started testentitybean in 3.868 seconds (jvm running for 4.854 ) 2016 - 10 - 24 11 : 49 : 22.676 info 700 --- [ main] org.cuckoo.report.brain.testentitybean : testbean(name=zhangsan, age= 123 ) 2016 - 10 - 24 11 : 49 : 22.691 info 700 --- [ thread- 2 ] o.s.w.c.s.genericwebapplicationcontext : closing org.springframework.web.context.support.genericwebapplicationcontext @4f80542f : startup date [mon oct 24 11 : 49 : 19 cst 2016 ]; root of context hierarchy |
大家可以注意到@slf4j替代掉了冗余的logger聲明語句, @data不必在使用getter/setter方法,其中tostring默認反射對象中的所有屬性,非常的好用。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.csdn.net/blueheart20/article/details/52909775