javascript
springboot中使用websocket_Spring Boot中Lombok使用
Lombok是什么
Lombok是一個(gè)java庫,它自動(dòng)插入到編輯器和構(gòu)建工具中,增強(qiáng)java的性能。不要再編寫另一個(gè)getter或equals方法,使用一個(gè)注釋,您的類有一個(gè)功能齊全的生成器,自動(dòng)記錄變量等等。簡(jiǎn)而言之,使用Lombok可以極大減輕編碼工作量。
官網(wǎng)地址:https://projectlombok.org
GitHub地址:https://github.com/rzwitserloot/lombok
Lombok安裝
IDEA Lombok插件安裝
在IDEA插件中心,查找Lombok,Install之后重啟IDEA。
插件安裝路徑:File->Settings->Plugins->Marketplace
IDEA安裝Lombok
項(xiàng)目引入依賴
我們以Spring Boot快速搭建中spring-boot-initializr-maven為例,添加Lombok依賴
<?xml ?version="1.0" encoding="UTF-8"?>? ?4.0.0? ?com.itlife? ?spring-boot-initializr-maven? ?1.0-SNAPSHOT? ?? ? ? ?org.springframework.boot? ? ? ?spring-boot-starter-parent? ? ? ?2.2.6.RELEASE? ? ? ??? ?? ?? ? ? ?1.8? ? ? ?1.16.18? ?? ?? ? ? ?? ? ? ? ? ?org.springframework.boot? ? ? ? ? ?spring-boot-starter-web? ? ? ?? ? ? ?? ? ? ?? ? ? ? ? ?org.projectlombok? ? ? ? ? ?lombok? ? ? ? ? ?${lombok.version}? ? ? ?? ?Lombok使用與測(cè)試
創(chuàng)建User實(shí)體類
package?com.itlife.initializr.maven.entity;import?lombok.AllArgsConstructor;import?lombok.Data;import?lombok.NoArgsConstructor;/*** @author zhongsy* @date 2020/3/27*/@Data@AllArgsConstructor@NoArgsConstructorpublic?class?User?{? ?/**? ??* 用戶Id? ??*/? ?private?Long?id;? ?/**? ??* 用戶名稱? ??*/? ?private?String?name;}- @Data:相當(dāng)于以下四個(gè)集合* @see Getter 自動(dòng)生成Getter方法
* @see Setter 自動(dòng)生成Setter
* @see RequiredArgsConstructor
* @see ToString 自動(dòng)生成toString()方法,默認(rèn)情況,按順序(以“,”分隔)打印你的類名稱以及每個(gè)字段。
* @see EqualsAndHashCode - @AllArgsConstructor 自動(dòng)生成一個(gè)包含所有變量的構(gòu)造方法
- @NoArgsConstructor 自動(dòng)生成一個(gè)無參構(gòu)造方法
創(chuàng)建UserController測(cè)試類
package?com.itlife.initializr.maven.controller;import?com.itlife.initializr.maven.entity.User;import?lombok.extern.slf4j.Slf4j;import?org.springframework.web.bind.annotation.GetMapping;import?org.springframework.web.bind.annotation.RequestMapping;import?org.springframework.web.bind.annotation.RequestParam;import?org.springframework.web.bind.annotation.RestController;/*** @author zhongsy* @date 2020/3/27*/@RestController@Slf4j@RequestMapping("user")public?class?UserController?{? ?@GetMapping("hello")? ?public?String?hello(User?user) {? ? ? ?log.info("收到用戶信息 {}",?user);? ? ? ?return?"hi, "?+?user?+?"!";? }}- @Slf4j:產(chǎn)生一個(gè)logger field,相當(dāng)于private static final Logger log = LoggerFactory.getLogger(UserController.class);
啟動(dòng)Spring Boot測(cè)試
- 在瀏覽器輸入測(cè)試地址 http://localhost:8080/user/hello?id=1&name=itLife,如果返回hi, User(id=1, name=itLife)!,表明使用成功
執(zhí)行結(jié)果
修改User實(shí)體類
package?com.itlife.initializr.maven.entity;import?lombok.AllArgsConstructor;import?lombok.Data;import?lombok.NoArgsConstructor;import?lombok.ToString;/*** @author zhongsy* @date 2020/3/27*/@Data@AllArgsConstructor@NoArgsConstructor@ToStringpublic?class?User?{? ?/**? ??* 用戶Id? ??*/? ?private?Long?id;? ?/**? ??* 用戶名稱? ??*/? ?private?String?name;? ?/**? ??* 測(cè)試重新toString與Lombok注解沖突? ??*/? ?@Override? ?public?String?toString() {? ? ? ?return?"User By Test {"?+? ? ? ? ? ? ? ?"id="?+?id?+? ? ? ? ? ? ? ?", name='"?+?name?+?'''?+? ? ? ? ? ? ? ?'}';? }}再次啟動(dòng)Spring Boot測(cè)試
- 在瀏覽器輸入測(cè)試地址 http://localhost:8080/user/hello?id=1&name=itLife,如果返回hi, User(id=1, name=itLife)!,表明使用成功
通過測(cè)試,如果Lombok與用戶所寫代碼有重復(fù)的話,以用戶代碼為準(zhǔn)
Lombok原理解析
反編譯User.class
Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package?com.itlife.initializr.maven.entity;import?java.beans.ConstructorProperties;public?class?User?{? ?private?Long?id;? ?private?String?name;? ?public?Long?getId() {? ? ? ?return?this.id;? }? ?public?String?getName() {? ? ? ?return?this.name;? }? ?public?void?setId(final?Long?id) {? ? ? ?this.id?=?id;? }? ?public?void?setName(final?String?name) {? ? ? ?this.name?=?name;? }? ?public?boolean?equals(final?Object?o) {? ? ? ?if?(o?==?this) {? ? ? ? ? ?return?true;? ? ? }?else?if?(!(o?instanceof?User)) {? ? ? ? ? ?return?false;? ? ? }?else?{? ? ? ? ? ?User?other?=?(User)o;? ? ? ? ? ?if?(!other.canEqual(this)) {? ? ? ? ? ? ? ?return?false;? ? ? ? ? }?else?{? ? ? ? ? ? ? ?Object?this$id?=?this.getId();? ? ? ? ? ? ? ?Object?other$id?=?other.getId();? ? ? ? ? ? ? ?if?(this$id?==?null) {? ? ? ? ? ? ? ? ? ?if?(other$id?!=?null) {? ? ? ? ? ? ? ? ? ? ? ?return?false;? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? }?else?if?(!this$id.equals(other$id)) {? ? ? ? ? ? ? ? ? ?return?false;? ? ? ? ? ? ? }? ? ? ? ? ? ? ?Object?this$name?=?this.getName();? ? ? ? ? ? ? ?Object?other$name?=?other.getName();? ? ? ? ? ? ? ?if?(this$name?==?null) {? ? ? ? ? ? ? ? ? ?if?(other$name?!=?null) {? ? ? ? ? ? ? ? ? ? ? ?return?false;? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? }?else?if?(!this$name.equals(other$name)) {? ? ? ? ? ? ? ? ? ?return?false;? ? ? ? ? ? ? }? ? ? ? ? ? ? ?return?true;? ? ? ? ? }? ? ? }? }? ?protected?boolean?canEqual(final?Object?other) {? ? ? ?return?other?instanceof?User;? }? ?public?int?hashCode() {? ? ? ?int?PRIME?=?true;? ? ? ?int?result?=?1;? ? ? ?Object?$id?=?this.getId();? ? ? ?int?result?=?result?*?59?+?($id?==?null???43?:?$id.hashCode());? ? ? ?Object?$name?=?this.getName();? ? ? ?result?=?result?*?59?+?($name?==?null???43?:?$name.hashCode());? ? ? ?return?result;? }? ?public?String?toString() {? ? ? ?return?"User(id="?+?this.getId()?+?", name="?+?this.getName()?+?")";? }? ?@ConstructorProperties({"id",?"name"})? ?public?User(final?Long?id,?final?String?name) {? ? ? ?this.id?=?id;? ? ? ?this.name?=?name;? }? ?public?User() {? }}反編譯UserController.class
Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package?com.itlife.initializr.maven.controller;import?com.itlife.initializr.maven.entity.User;import?org.slf4j.Logger;import?org.slf4j.LoggerFactory;import?org.springframework.web.bind.annotation.GetMapping;import?org.springframework.web.bind.annotation.RequestMapping;import?org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping({"user"})public?class?UserController?{? ?private?static?final?Logger?log?=?LoggerFactory.getLogger(UserController.class);? ?public?UserController() {? }? ?@GetMapping({"hello"})? ?public?String?hello(User?user) {? ? ? ?log.info("收到用戶信息 {}",?user);? ? ? ?return?"hi, "?+?user?+?"!";? }}工作原理說明
通過反編譯即可知道,Lombok在生成class文件的時(shí)候,會(huì)根據(jù)注解進(jìn)行代碼生成相應(yīng)代碼,使得我們代碼更加簡(jiǎn)潔。其核心之處就是對(duì)于注解的解析上。JDK5引入了注解的同時(shí),也提供了兩種解析方式。分別是運(yùn)行時(shí)解析和編譯時(shí)解析兩種。具體如何實(shí)現(xiàn),可以自行查閱官網(wǎng)或者進(jìn)行源碼閱讀。
總結(jié)
優(yōu)點(diǎn)
缺點(diǎn)
使用建議
項(xiàng)目中除非是老項(xiàng)目,不然建議使用。新的項(xiàng)目,建議采用。使用Lombok可以極大減少代碼量,更加專注于查看類的屬性。如果對(duì)于有特殊性要求,可以在使用Lombok的同時(shí)自行寫相應(yīng)的方法。
總結(jié)
以上是生活随笔為你收集整理的springboot中使用websocket_Spring Boot中Lombok使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java spring事务管理系统_Ja
- 下一篇: 适用所有服务器的全站301重定向跳转教程