Lombok的使用
1,了解Lombok
看到网上的大佬都在使用Lombok插件我也就跟风学习一下。
首先,当然看的是官网。官网是这样解释的:
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
这里是说Lombok项目是一个会自动插入到你的编译器和构建工具中的Java库,从而是你的Java更加生动有趣。不需要再写任何的getter和equals方法,你的带有注解的有功能全面的生成器,能够自动化你的日志记录等
有关Lombok的学习点击
2,安装Lombok
如果用IDEA编辑器的最新版(2020.3)的话,会发现该编译器已经内置了Lombok插件,直接俄使用就可以。使用maven开发的话直接在pom.xml文件中添加依赖即可。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
3,Lombok常用注解
-
@Getter and @Setter 自动为类生成属性相对应的getXx()方法以及setXx()方法
public class User { @Setter@Getter private String name ; private int id; }
- @Data注解:这个注解包含范围最广,使用该注解会自动生成一个无参构造函数,属性的get和set方法,toString方法,hashCode方法,equals等,如下:
import lombok.*;
@Data
public class User {
private String name ;
private int id;
}
-
@NoArgsConstructor注解: 生成该类的无参构造方法
-
@AllArgsConstructor注解 :生成对应的有参构造方法,这里需要注意的是,当同时使用了@data和@AllArgsConstructor注解时,此时是没有无参构造函数的,需要自行添加 @NoArgsConstructor注解。如下:
@Data
@AllArgsConstructor
public class User {
private String name ;
private int id;
}
-
@ToString注解 :生成toString方法
-
@EqualsAndHashCode注解:使用此注解会自动重写对应的equals方法和hashCode方法
-
@NonNull注解: 使用该注解后便会自动对方法中该参数值进行判空。 为了方便理解此处直接上代码
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@NonNull private String name ;
private int id;
}
相当于:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import lombok.NonNull;
public class User {
@NonNull
private String name;
private int id;
public User(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked non-null but is null");
} else {
this.name = name;
}
}
@NonNull
public String getName() {
return this.name;
}
public int getId() {
return this.id;
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked non-null but is null");
} else {
this.name = name;
}
}
public void setId(int id) {
this.id = id;
}
public boolean equals(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 if (this.getId() != other.getId()) {
return false;
} else {
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(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getId();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "User(name=" + this.getName() + ", id=" + this.getId() + ")";
}
}
4,优缺点
优点:
- 减少了大量的模板代码。
- 提高了代码的开发效率。
缺点:
- 当有n个属性时不能实现1~n-1个参数的重载
- 一定程度上降低了代码的可读性