一、lombok介绍
- Lombok官方网站
- Lombok的初衷是为了减少样板代码(如get/set方法),他能让我们的java代码显得更加的简洁,快速。它使用的解决方式是利用注解来代替我们编写的冗余代码,如下,我们来感受一下
在传统编写JavaBean的时候,我们经常这么写:
public class Person {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
我们可以观察到,仅仅是两个字段,其get/set方法就占用了大部分的代码行,并且这个类看起来比较混乱(当然,两个字段可能你觉得还是比较简洁,可是你想想当你有10个字段?20个字段?那时候你会觉得是否代码太过臃肿)。
请看lombok为我们带来的解决方式:
@Data
@ToString
public class Person {
private Long id;
private String name;
}
编译之后的class如下:
public class Person {
private Long id;
private String name;
public Person() {
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)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(Object other) {
return other instanceof Person;
}
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 "Person(id=" + this.getId() + ", name=" + this.getName() + ")";
}
}
二、lombok的下载与安装
2.2 lombok相关jar包下载:
下载地址https://projectlombok.org/download
下载后如图所示:
图2.1 lombok.jar
下载后导入你的工程项目即可使用
2.1 lombok IDEA 插件安装
为什么要安装插件呢?原则上来讲,我们导入相关lombok.jar包就可以运行程序了,也不会有运行报错,如图所示:
图2.2 运行实例通过,但编译器提示错误
但是细心的朋友会发现,我们的编译器(如本文用的IDEA),它会提示你找不到对应的方法(当然IDEA也不会提示你有get/set方法),但是编译能通过,程序也能正常运行
这是为什么呢?
原因是我们使用lombok注解的时候,在程序编译.java文件时才会将get/set方法编译进去。有兴趣的朋友可以查看编译完成之后的.class文件,即可明白
那怎么解决这个问题呢?
这时候我们就需要安装一个lombok插件,他会帮助编译器识别哪些代码以及方法使用了lombok(插件名称:Lombok Plugin)
不知道怎么安装插件的,可以借鉴一下这篇文章:IntelliJ IDEA 常用插件推荐
这里注意一下,安装完成后可能要重启才有效(即图2.2编译器检查代码正常不报错):
图2.3 lombok
2.2 IDEA中引入lombok插件后编译时实体类时提示无get/set方法
在第一次使用lombok的时候,发现即便我加了注解,IDEA语法检查也不报错,但是编译之后提示没有get/set方法。查看class文件之后确实没有,后来找到解决方案如下:
File —— Settings… 找到如下:
图2.4 修改配置
File —— Project Structure… 找到如下:
图2.5 修改配置
解决。。。
三、lombok的基本使用
3.1 @Getter / Setter
- 作用:生成对应的get/set方法——如果要修改方法修饰符可以设置AccessLevel的值
- 说明:
你可以用@Getter / @Setter注释任何字段(当然也可以注释到类上的),让lombok自动生成默认的getter / setter方法。
默认生成的方法是public的,如果要修改方法修饰符可以设置AccessLevel的值,例如:@Getter(access = AccessLevel.PROTECTED)
作者:缓慢移动的蜗牛
链接:https://www.jianshu.com/p/365ea41b3573
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
- 使用案例:
public class Person {
@Getter
@Setter
private boolean employed = true;
@Setter(AccessLevel.PROTECTED)
private String name;
}
- 编译源码:
private boolean employed = true;
private String name;
public boolean isEmployed() {
return employed;
}
public void setEmployed(final boolean employed) {
this.employed = employed;
}
protected void setName(final String name) {
this.name = name;
}
3.2 @NonNull
- 作用:标记属性不能为空
- 使用案例:
public class Person {
@Getter @Setter @NonNull
private List<Person> members;
}
- 编译源码:
@NonNull
private List<Person> members;
public Family(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
@NonNull
public List<Person> getMembers() {
return members;
}
public void setMembers(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members is marked @NonNull but is null");
this.members = members;
}
3.3 @toString
- 作用:重写toString方法
- 说明:
生成toString()方法,默认情况下,它会按顺序(以逗号分隔)打印你的类名称以及每个字段。可以这样设置不包含哪些字段@ToString(exclude = “id”) / @ToString(exclude = {“id”,”name”})
如果继承的有父类的话,可以设置callSuper 让其调用父类的toString()方法,例如:@ToString(callSuper = true)
作者:缓慢移动的蜗牛
链接:https://www.jianshu.com/p/365ea41b3573
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
- 使用案例:
@ToString
public class Person {
private Long id;
private String name;
}
- 编译源码:
private Long id;
private String name;
public Person() {
}
public String toString() {
return "Person(id=" + this.id + ", name=" + this.name + ")";
}
3.4 @EqualsAndHashCode
- 作用:给类增加equals和hashCode方法 (比较复杂,详见官方网址)
- 说明:
生成hashCode()和equals()方法,默认情况下,它将使用所有非静态,非transient字段。但可以通过在可选的exclude参数中来排除更多字段。或者,通过在parameter参数中命名它们来准确指定希望使用哪些字段。
- 使用案例:
@EqualsAndHashCode(callSuper = false, exclude = {"address", "city"})
public class Person{
private Long id;
private String name;
private String address;
private String city;
}
- 编译源码
public class Person {
private Long id;
private String name;
private String address;
private String city;
public Person() {
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$id = this.id;
Object other$id = other.id;
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
Object this$name = this.name;
Object other$name = other.name;
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 Person;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.id;
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $name = this.name;
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
}
3.5 @Data
@Data可能是最频繁注释的注释使用Lombok工具集,因为它集合了多个功能,如下:
- @ToString
- @EqualsAndHashCode
- @Getter
- @Setter
使用案例:
@Data
public class Person{
private Long id;
private String name;
private String address;
private String city;
}
编译源码:
private Long id;
private String name;
private String address;
private String city;
public Person() {
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public String getAddress() {
return this.address;
}
public String getCity() {
return this.city;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setCity(String city) {
this.city = city;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if (!other.canEqual(this)) {
return false;
} else {
label59: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label59;
}
} else if (this$id.equals(other$id)) {
break label59;
}
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;
}
Object this$address = this.getAddress();
Object other$address = other.getAddress();
if (this$address == null) {
if (other$address != null) {
return false;
}
} else if (!this$address.equals(other$address)) {
return false;
}
Object this$city = this.getCity();
Object other$city = other.getCity();
if (this$city == null) {
if (other$city != null) {
return false;
}
} else if (!this$city.equals(other$city)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Person;
}
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());
Object $address = this.getAddress();
result = result * 59 + ($address == null ? 43 : $address.hashCode());
Object $city = this.getCity();
result = result * 59 + ($city == null ? 43 : $city.hashCode());
return result;
}
public String toString() {
return "Person(id=" + this.getId() + ", name=" + this.getName() + ", address=" + this.getAddress() + ", city=" + this.getCity() + ")";
}
3.6 @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor
@NoArgsConstructor生成无参构造方法
@RequiredArgsConstructor生成构造方法(可能带参数也可能不带参数)
注意:@RequiredArgsConstructor(staticName=”methodName”)的形式生成一个指定名称的静态方法,返回一个调用其相应的构造方法产生的对象
@AllArgsConstructor 生成一个全参数的构造方法
使用案例:
@RequiredArgsConstructor(staticName = "getPerson")
public class Person{
private Long id;
private String name;
private String address;
private String city;
}
编译源码:
public class Person {
private Long id;
private String name;
private String address;
private String city;
private Person() {
}
public static Person getPerson() {
return new Person();
}
}