一、Lombok 的简单介绍和使用
1、 添加 Lombok 的依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
二、 常用 Lombok 注解实战
2.1、@Getter / @Setter
@Getter
@Setter
public class SampleLombok {
private String username;
private String pwd;
}
字节码文件反编译后的内容如下:
package com.example.demo.lombokdemo;
public class SampleLombok {
private String username;
private String pwd;
public SampleLombok() {
}
public String getUsername() {
return this.username;
}
public String getPwd() {
return this.pwd;
}
public void setUsername(final String username) {
this.username = username;
}
public void setPwd(final String pwd) {
this.pwd = pwd;
}
}
对于部分字段的特殊需求
public class SampleLombok {
@Getter private String username;
@Getter @Setter private String pwd;
}
字节码文件反编译后的内容如下:
package com.example.demo.lombokdemo;
public class SampleLombok {
private String username;
private String pwd;
public SampleLombok() {
}
public String getUsername() {
return this.username;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2.2、 @ToString、@AllArgsConstructor、@NoArgsConstructor、@EqualsAndHashCode
@ToString
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class SampleLombok {
private String username;
private String pwd;
}
字节码文件反编译后的内容如下:
package com.example.demo.lombokdemo;
public class SampleLombok {
private String username;
private String pwd;
public String toString() {
return "SampleLombok(username=" + this.username + ", pwd=" + this.pwd + ")";
}
public SampleLombok(final String username, final String pwd) {
this.username = username;
this.pwd = pwd;
}
public SampleLombok() {
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof SampleLombok)) {
return false;
} else {
SampleLombok other = (SampleLombok)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$username = this.username;
Object other$username = other.username;
if (this$username == null) {
if (other$username != null) {
return false;
}
} else if (!this$username.equals(other$username)) {
return false;
}
Object this$pwd = this.pwd;
Object other$pwd = other.pwd;
if (this$pwd == null) {
if (other$pwd != null) {
return false;
}
} else if (!this$pwd.equals(other$pwd)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof SampleLombok;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $username = this.username;
int result = result * 59 + ($username == null ? 43 : $username.hashCode());
Object $pwd = this.pwd;
result = result * 59 + ($pwd == null ? 43 : $pwd.hashCode());
return result;
}
}
2.3、@NonNull
@NonNull
public String getPwd(@NonNull Person p){
return p.getPwd();
}
字节码文件反编译后的内容如下:
public String getPwd(@NonNull Person p) {
if (p == null) {
throw new NullPointerException("person is marked non-null but is null");
} else {
return p.getPwd();
}
}
2.4、@Slf4j
2.5、 @Accessors(fluent = true) 、 @Accessors(chain = true)
2.6、@Builder(重点)
@Builder
public class Student {
private String name;
private int age;
}
等价于以下内容:
Person p = Person.builder().name("Rookie").age(24).build();