——springboot依赖lombok插件、lombok常用注解
1 lombok插件
1.1 lombok插件简介
官方介绍如下:

意思是:lombok是一个能自动插入到编辑器和构建工具的java库。目的是为了简化java代码,开发者不需要写setter、getter和构造方法等,只需要几个或多个lombok注解就能实现。
Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。例如开发中经常需要写的pojo,都需要花时间去添加相应的getter、setter、构造器和equals等方法,当属性多时会出现大量的getter/setter方法,维护起来很麻烦,也容易出错。
1.2 springboot依赖lombok插件
springboot中只需要依赖lombok的jar包就可使用Lombok。
(1)方法一:可以在官网(Download)下载jar包:

(2)方法二: 可以使用maven添加依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
2 lombok常用注解
2.1 @NotNull
@NotNull注解如果设置在一个属性上,lombok将在自动生成的方法/构造函数体的开头插入一个空检查,抛出NullPointerException,并将参数的名称作为message。
lombok注解实现:
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
@NoArgsConstructor
public static class NoArgsExample {
@NonNull private String field;
}
}
等价代码:
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
private ConstructorExample(T description) {
//空检查
if (description == null) throw new NullPointerException("description");
this.description = description;
}
public static <T> ConstructorExample<T> of(T description) {
return new ConstructorExample<T>(description);
}
@java.beans.ConstructorProperties({"x", "y", "description"})
protected ConstructorExample(int x, int y, T description) {
//空检查
if (description == null) throw new NullPointerException("description");
this.x = x;
this.y = y;
this.description = description;
}
public static class NoArgsExample {
@NonNull private String field;
public NoArgsExample() {
}
}
}
2.2 @NoArgsConstructor、@RequiredArgsConstructor和@AllArgsConstructor
2.2.1 @NoArgsConstructor
生成不带参数的构造方法,即:无参构造方法。@NoArgsConstructor等价于:
public MessageInfo() {
}
如果存在被final修饰的属性,那么会编译失败。

可以设置force属性值为true,来避免编译错误。但是所有final修饰的属性会被初始化为final字段初始化为:0 或null 或 false。也就是说包装类型会被初始化为null,简单类型初始化为0或false。


2.2.2 @RequiredArgsConstructor
只为final / @non-null修饰字段(不包括static修饰的)生成带参数的构造方法。


2.2.3 @AllArgsConstructor
描述:为所有字段(包括final修饰的,不包括static修饰的)生成带参数的构造方法,即:全参数构造方法。


2.3 @Getter和@Setter
lombok官方文档的描述:
![]()
lombok注解实现:
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
public class GetterSetterExample {
/**
* Age of the person. Water is wet.
*
* @param age New value for this person's age. Sky is blue.
* @return The current value of this person's age. Circles are round.
*/
@Getter @Setter private int age = 10;
/**
* Name of the person.
* -- SETTER --
* Changes the name of this person.
*
* @param name The new value.
*/
@Setter(AccessLevel.PROTECTED) private String name;
@Override public String toString() {
return String.format("%s (age: %d)", name, age);
}
}
等价代码:
public class GetterSetterExample {
/**
* A

Lombok是一个能够自动插入编辑器和构建工具的Java库,旨在简化Java代码,减少getter、setter等样板代码的编写。通过@NotNull、@NoArgsConstructor、@RequiredArgsConstructor、@AllArgsConstructor、@Getter、@Setter等注解,可以快速生成构造方法、getter和setter等。此外,Lombok还提供了@ToString、@SneakyThrows、@Synchronized等注解,用于生成toString方法、处理异常和同步方法。@Data和@Value注解则能自动生成类的完整逻辑,包括equals和hashCode。Lombok的使用极大地提高了开发效率和代码整洁度。
2477

被折叠的 条评论
为什么被折叠?



