地址:https://projectlombok.org/
官网简介:Project Lombok是一个Java库,可以自动插入到您的编辑器中并构建工具,为您的Java增添趣味。 再也不用再写另一个
getter 或 equals 方法了,只要有一个注释,你的类就有了一个功能齐全的生成器、自动化你的日志记录变量等等。
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.16.20version>
<scope>providedscope>
dependency>
首先是最常用的@Data,使用方式往往就是在类上注释,它会自动引入:
1.@Getter和@Setter
2.@ToString
3.@EqualsAndHashCode
4.@RequiredArgsConstructor
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private String name;
private Integer age;
}
编译后打开class可以看到添加了@Data注解后的效果:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public String getName() {
return this.name;
}
public Integer getAge() {
return this.age;
}
public void setName(final String name) {
this.name = name;
}
public void setAge(final Integer age) {
this.age = age;
}
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$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$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
return result;
}
public String toString() {
return "User(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
/*这个构造函数是@AllArgsConstructor生成的*/
public User(final String name, final Integer age) {
this.name = name;
this.age = age;
}
}
在使用的过程中,对这三个注解的效果有一些没分清,所以各使用了一次查看了编译后的代码:
@NoArgsConstructor:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public User() {
}
}
@RequiredArgsConstructor:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public User() {
}
}
@AllArgsConstructor:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public User(final String name, final Integer age) {
this.name = name;
this.age = age;
}
}
可见@NoArgsConstructor和@RequiredArgsConstructor在编译后生成的构造函数是一样的,那区别在哪里呢?
经过查找资料:@NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor的区别以及在springboot常用地方
@NoArgsConstructor:生成无参的构造方法。
@AllArgsConstructor:生成该类下全部属性的构造方法。
@RequiredArgsConstructor:生成该类下被final修饰或者non-null字段生成一个构造方法。
也就是说@RequiredArgsConstructor只会构造有final或者no-null修饰的字段
当我对User类的属性进行final或non-null修饰时,@NoArgsConstructor和@RequiredArgsConstructor构造出来的构造函数才会有区别:
package com.hgh.boot.bean;
public class User {
private final String name;
private Integer age;
public User(final String name) {
this.name = name;
}
}