Java開發(fā)神器Lombok安裝與使用詳解
安裝
Lombok的安裝分兩部分:Idea插件的安裝和maven中pom文件的導(dǎo)入。
Idea插件的安裝
點(diǎn)擊設(shè)置,選擇插件,在Idea的插件配置中搜索Lombok(需要聯(lián)網(wǎng))或官網(wǎng)下載本地安裝,點(diǎn)擊初始化安裝

在插件的描述中也能夠看到它支持的注解。
maven中pom文件的導(dǎo)入
第二步,引入pom中依賴,當(dāng)前最細(xì)版本1.18.10。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>如果是通過Idea創(chuàng)建Spring Boot項(xiàng)目,可在創(chuàng)建項(xiàng)目時(shí)直接在“Developer Tool”中選擇Lombok。
完成了以上兩步,就可以在代碼中使用該款神器了。
使用
這是官網(wǎng)給出的所支持的注解,在這我們也能查看:https://projectlombok.org/features/all
在這里給出常用的注解及其介紹:
val
使用val作為局部變量聲明的類型,而不是實(shí)際寫入類型。 執(zhí)行此操作時(shí),將從初始化表達(dá)式推斷出類型。
public Map<String, String> getMap() {
val map = new HashMap<String, String>();
map.put("1", "a");
return map;
}效果如下:
public Map<String, String> getMap() {
HashMap<String, String> map = new HashMap();
map.put("1", "a");
return map;
}也就是說在局部變量中,Lombok幫你推斷出具體的類型,但只能用于局部變量中。
@Data
@Data最常用的注解之一。注解在類上,提供該類所有屬性的getter/setter方法,還提供了equals、canEqual、hashCode、toString方法。
就是開發(fā)人員不用手寫相應(yīng)的方法,而Lombok會幫你生成上述所有方法。
使用@Data示例如下,最直觀的就是不用寫getter/setter方法。
@Data
public class Demo {
private int id;
private String remark;
}我們看該類編譯之后是什么樣子。
public class Demo {
private int id;
private String remark;
public Demo() {
}
public int getId() {
return this.id;
public String getRemark() {
return this.remark;
public void setId(final int id) {
this.id = id;
public void setRemark(final String remark) {
this.remark = remark;
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Demo)) {
return false;
} else {
Demo other = (Demo)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
} else {
Object this$remark = this.getRemark();
Object other$remark = other.getRemark();
if (this$remark == null) {
if (other$remark != null) {
return false;
}
} else if (!this$remark.equals(other$remark)) {
return false;
}
return true;
}
}
protected boolean canEqual(final Object other) {
return other instanceof Demo;
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 this.getId();
Object $remark = this.getRemark();
result = result * 59 ($remark == null ? 43 : $remark.hashCode());
return result;
public String toString() {
return "Demo(id=" this.getId() ", remark=" this.getRemark() ")";
}官網(wǎng)給的更加詳細(xì),這里只展現(xiàn)一部分,例如下圖是官網(wǎng)給的例子:
鏈接:https://projectlombok.org/features/Data

@Getter/@Setter
作用于屬性上,為該屬性提供getter/setter方法;
作用與類上,為該類所有的屬性提供getter/setter方法, 都提供默認(rèn)構(gòu)造方法。
使用@Getter/@Setter示例如下:
public class GetterSetterExample {
@Getter @Setter private int age = 10;
@Setter(AccessLevel.PROTECTED) private String name;
@Override public String toString() {
return String.format("%s (age: %d)", name, age);
}
}
編譯后就是這個(gè)樣子:
public class GetterSetterExample {
private int age = 10;
private String name;
@Override public String toString() {
return String.format("%s (age: %d)", name, age);
}
public int getAge() {
return age;
public void setAge(int age) {
this.age = age;
protected void setName(String name) {
this.name = name;
}
其余例子就不展示了,大家可以去官網(wǎng)查看詳細(xì)的,這里只給出作用。
@Log4j
作用于類上,為該類提供一個(gè)屬性名為log的log4j日志對象。
@Log4j
public class Demo {
}
該屬性一般使用于Controller、Service等業(yè)務(wù)處理類上。與此注解相同的還有@Log4j2,顧名思義,針對Log4j2。
@AllArgsConstructor
作用于類上,為該類提供一個(gè)包含全部參的構(gòu)造方法,注意此時(shí)默認(rèn)構(gòu)造方法不會提供。
@AllArgsConstructor
public class Demo {
private int id;
private String remark;
}
效果如下:
public class Demo {
private int id;
private String remark;
public Demo(final int id, final String remark) {
this.id = id;
this.remark = remark;
}
}
@NoArgsConstructor
作用于類上,提供一個(gè)無參的構(gòu)造方法。
可以和@AllArgsConstructor同時(shí)使用,此時(shí)會生成兩個(gè)構(gòu)造方法:無參構(gòu)造方法和全參構(gòu)造方法。
@EqualsAndHashCode
作用于類上,生成equals、canEqual、hashCode方法。
具體效果參看最開始的@Data效果,也可以參考官網(wǎng)。
@NonNull
作用于屬性上,提供關(guān)于此參數(shù)的非空檢查,如果參數(shù)為空,則拋出空指針異常。
使用方法:
public class Demo {
@NonNull
private int id;
private String remark;
}
@RequiredArgsConstructor
作用于類上,由類中所有帶有@NonNull注解或者帶有final修飾的成員變量作為參數(shù)生成構(gòu)造方法。
@Cleanup
作用于變量,保證該變量代表的資源會被自動關(guān)閉,默認(rèn)調(diào)用資源的close()方法,如果該資源有其它關(guān)閉方法,可使用@Cleanup(“methodName”)來指定。
public void jedisExample(String[] args) {
try {
@Cleanup Jedis jedis = redisService.getJedis();
} catch (Exception ex) {
logger.error(“Jedis異常:”,ex)
}
}
效果相當(dāng)于:
public void jedisExample(String[] args) {
Jedis jedis= null;
try {
jedis = redisService.getJedis();
} catch (Exception e) {
logger.error(“Jedis異常:”,ex)
} finally {
if (jedis != null) {
try {
jedis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@ToString
作用于類上,生成包含所有參數(shù)的toString方法。見@Data中toString方法。
@Value
作用于類上,會生成全參數(shù)的構(gòu)造方法、getter方法、equals、hashCode、toString方法。
與@Data相比多了全參構(gòu)造方法,少了默認(rèn)構(gòu)造方法、setter方法和canEqual方法。
該注解需要注意的是:會將字段添加上final修飾,個(gè)人感覺此處有些失控,不太建議使用。
@SneakyThrows
作用于方法上,相當(dāng)于把方法內(nèi)的代碼添加了一個(gè)try-catch處理,捕獲異常catch中用Lombok.sneakyThrow(e)拋出異常。使用@SneakyThrows(BizException.class)指定拋出具體異常。
@SneakyThrows
public int getValue(){
int a = 1;
int b = 0;
return a/b;
}
效果如下:
public int getValue() {
try {
int a = 1;
int b = 0;
return a / b;
} catch (Throwable var3) {
throw var3;
}
}
@Synchronized
作用于類方法或?qū)嵗椒ㄉ?,效果與synchronized相同。
區(qū)別在于鎖對象不同,對于類方法和實(shí)例方法,synchronized關(guān)鍵字的鎖對象分別是類的class對象和this對象,而@Synchronized的鎖對象分別是私有靜態(tài)final對象lock和私有final對象lock。也可以指定鎖對象。
public class FooExample {
private final Object readLock = new Object();
@Synchronized
public static void hello() {
System.out.println("world");
}
@Synchronized("readLock")
public void foo() {
System.out.println("bar");
}
}
效果相當(dāng)于如下:
public class FooExample {
private static final Object $LOCK = new Object[0];
private final Object readLock = new Object();
public static void hello() {
synchronized($LOCK) {
System.out.println("world");
}
}
public void foo() {
synchronized(readLock) {
System.out.println("bar");
}
}@Builder
作用于類上,如果你喜歡使用Builder的流式操作,那么@Builder可能是你喜歡的注解了。
使用方法:
@Builder
public class Demo {
private int id;
private String remark;
}
效果如下:
public class Demo {
private int id;
private String remark;
Demo(final int id, final String remark) {
this.id = id;
this.remark = remark;
}
public static Demo.DemoBuilder builder() {
return new Demo.DemoBuilder();
public static class DemoBuilder {
private int id;
private String remark;
DemoBuilder() {
}
public Demo.DemoBuilder id(final int id) {
this.id = id;
return this;
public Demo.DemoBuilder remark(final String remark) {
this.remark = remark;
public Demo build() {
return new Demo(this.id, this.remark);
public String toString() {
return "Demo.DemoBuilder(id=" this.id ", remark=" this.remark ")";
}我們可以看到,在該類內(nèi)部提供了DemoBuilder類用來處理具體的流式操作。同時(shí)提供了全參的構(gòu)造方法。
到此這篇關(guān)于Java開發(fā)神器Lombok安裝與使用的文章就介紹到這了,更多相關(guān)Java Lombok安裝與使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot簡單使用SpringData的jdbc和durid
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著SpringBoot簡單使用SpringData的jdbc和durid,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring中@EnableScheduling注解的工作原理詳解
這篇文章主要介紹了Spring中@EnableScheduling注解的工作原理詳解,@EnableScheduling是 Spring Framework 提供的一個(gè)注解,用于啟用Spring的定時(shí)任務(wù)(Scheduling)功能,需要的朋友可以參考下2024-01-01
IntelliJ?IDEA?2022.1.1?沒有CVS的過程分析
這篇文章主要介紹了IntelliJ?IDEA?2022.1.1?沒有CVS的過程解析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突
這篇文章主要介紹了 Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突的相關(guān)資料,需要的朋友可以參考下2017-01-01
springboot 動態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid)
這篇文章主要介紹了springboot 動態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
springboot項(xiàng)目中使用Swagger的簡單示例
大趨勢下目前很多的項(xiàng)目都采用了前后端分離的方式進(jìn)行開發(fā),最近我接觸到的項(xiàng)目大多數(shù)都是采用了前后端分離的方式進(jìn)行開發(fā),下面這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目中使用Swagger的簡單示例,需要的朋友可以參考下2023-04-04

