我们在开发测试代码过程中,通常都会定义大量的 JavaBean ,然后通过IDE 去生成其属性的构造器、getter、setter、equals、hashcode、toString 方法,当要增加属性或者对某个属性进行改变时,比如命名、类型等,都需要重新去生成上面提到的这些方法。这样重复的劳动没有任何意义,Lombok 里面的注解可以轻松解决这些问题。
Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO)。它通过注解实现这一目的。
官方地址:https://projectlombok.org/
github:https://github.com/rzwitserloot/lombok
自从Java 6起,javac 就支持“JSR 269 Pluggable Annotation Processing API”规范,只要程序实现了该API,就能在javac运行的时候得到调用。Lombok就是一个实现了"JSR 269 API"的程序。
在使用 javac 的过程中,它产生作用的具体流程如下:
本文演示 IntelliJ IDEA
File>Settings>Plugins
Browser epositories
Lombok Plugin
Install plugin
点击 File--Settings
设置界面,开启 Annocation Processors
开启该项是为了让 Lombok 注解在编译阶段起到作用。
最后,重启 IntelliJ IDEA
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
@Data 标签,生成 getter/setter toString() 等方法 @NonNull` : 让你不在担忧并且爱上 NullPointerException @CleanUp : 自动资源管理:不用再在 finally 中添加资源的 close 方法 @Setter/@Getter : 自动生成set和get方法 @ToString : 自动生成 toString 方法 @EqualsAndHashcode : 从对象的字段中生成 hashCode 和 equals 的实现 @NoArgsConstructor/@RequiredArgsConstructor/@AllArgsConstructor 自动生成构造方法 @Data : 自动生成 set/get 方法,toString 方法,equals 方法,hashCode方法,不带参数的构造方法 @Value : 用于注解 final 类 @Builder : 产生复杂的构建器 api 类 @SneakyThrows : 异常处理(谨慎使用) @Synchronized : 同步方法安全的转化 @Getter(lazy=true) : @Log : 支持各种logger对象,使用时用对应的注解,如:@Log4j
更多注解说明:https://projectlombok.org/features/index.html
使用lombok
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Demo {
String code;
String name;
}
等同于
public class Demo {
String code;
String name;
public static DemoBuilder builder() {
return new DemoBuilder();
}
public String getCode() {
return this.code;
}
public String getName() {
return this.name;
}
public void setCode(String code) {
this.code = code;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Demo))
return false;
Demo other = (Demo) o;
if (!other.canEqual(this))
return false;
Object this$code = getCode();
Object other$code = other.getCode();
if (this$code == null ? other$code != null : !this$code.equals(other$code))
return false;
Object this$name = getName();
Object other$name = other.getName();
return this$name == null ? other$name == null : this$name.equals(other$name);
}
protected boolean canEqual(Object other) {
return other instanceof Demo;
}
public int hashCode() {
int PRIME = 59;
int result = 1;
Object $code = getCode();
result = result * 59 + ($code == null ? 43 : $code.hashCode());
Object $name = getName();
return result * 59 + ($name == null ? 43 : $name.hashCode());
}
public String toString() {
return "Demo(code=" + getCode() + ", name=" + getName() + ")";
}
public Demo() {
}
public Demo(String code, String name) {
this.code = code;
this.name = name;
}
public static class DemoBuilder {
private String code;
private String name;
public DemoBuilder code(String code) {
this.code = code;
return this;
}
public DemoBuilder name(String name) {
this.name = name;
return this;
}
public Demo build() {
return new Demo(this.code, this.name);
}
public String toString() {
return "Demo.DemoBuilder(code=" + this.code + ", name=" + this.name + ")";
}
}
}
使用 @Slf4j
注解
@Slf4j
public class LogExampleOther {
public static void main(String... args) {
log.error("Something else is wrong here");
}
}
常规的写法
public class LogExampleOther {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);
public static void main(String... args) {
log.error("Something else is wrong here");
}
}
@NonNull
注解,Null 即是罪恶
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
this.name = person.getName();
}
}
常规的写法
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
if (person == null) {
throw new NullPointerException("person");
}
this.name = person.getName();
}
}
@Builder
注解,builder 是现在比较推崇的一种构建值对象的方式。
@Builder
public class BuilderExample {
private String name;
private int age;
@Singular private Set<String> occupations;
}
常规写法:
public class BuilderExample {
private String name;
private int age;
private Set<String> occupations;
BuilderExample(String name, int age, Set<String> occupations) {
this.name = name;
this.age = age;
this.occupations = occupations;
}
public static BuilderExampleBuilder builder() {
return new BuilderExampleBuilder();
}
public static class BuilderExampleBuilder {
private String name;
private int age;
private java.util.ArrayList<String> occupations;
BuilderExampleBuilder() {
}
public BuilderExampleBuilder name(String name) {
this.name = name;
return this;
}
public BuilderExampleBuilder age(int age) {
this.age = age;
return this;
}
public BuilderExampleBuilder occupation(String occupation) {
if (this.occupations == null) {
this.occupations = new java.util.ArrayList<String>();
}
this.occupations.add(occupation);
return this;
}
public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
if (this.occupations == null) {
this.occupations = new java.util.ArrayList<String>();
}
this.occupations.addAll(occupations);
return this;
}
public BuilderExampleBuilder clearOccupations() {
if (this.occupations != null) {
this.occupations.clear();
}
return this;
}
public BuilderExample build() {
// complicated switch statement to produce a compact properly sized immutable set omitted.
// go to https://projectlombok.org/features/Singular-snippet.html to see it.
Set<String> occupations = ...;
return new BuilderExample(name, age, occupations);
}
@java.lang.Override
public String toString() {
return "BuilderExample.BuilderExampleBuilder(name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
}
}
}
总之,省了多少事!!!