一、认识Lombok
1、概述:
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官网
- 中文意思是:Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发。通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量;也就是说可以通过注解的方式简化代码;
2、Lombok注解介绍
@Getter/@Setter 注解在属性上,自动生成get、set方法;Getter(所有字段)
Setter (所有非final字段)
@Data 注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法,Data一个注解也就完成了下面五个注解完成的事情
( @ToString
@EqualsAndHashCode
@Getter(所有字段)
@Setter (所有非final字段)
@RequiredArgsConstructor
)
@AIlArgSConstructor 注解在类上;为类提供一个全参的构造方法
@RequiredArgsConstructor and
@NoArgsConstructor 注解在类上;为类提供一个无参的构造方法
@NonNull 是否为空判断,为空则抛出java.lang.NullPointerException
@Log4j 日志注解
@SLf4j 日志注解
@Builder 生成建造者模式(Builder)
@Cleanup 注释可用于确保已分配的资源被释放,生成的代码会把变量用try{}包围,并在finallly块中调用close()
@SneakyThrows 使用try{}catch捕获异常
二、应用
1、添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
2、安装Lombok
File–setting–Editor–Plugins–Lombok
3、使用
- @Data 例子-使用注解后
@Data
public class Example {
private int foo;
private final String bar;
}
等价源码-使用前
public class Example {
private int foo;
private final String bar;
public Example(String bar) {
this.bar = bar;
}
public int getFoo() {
return this.foo;
}
public String getBar() {
return this.bar;
}
public void setFoo(int foo) {
this.foo = foo;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Example)) {
return false;
} else {
Example other = (Example)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getFoo() != other.getFoo()) {
return false;
} else {
Object this$bar = this.getBar();
Object other$bar = other.getBar();
if (this$bar == null) {
if (other$bar != null) {
return false;
}
} else if (!this$bar.equals(other$bar)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Example;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getFoo();
Object $bar = this.getBar();
result = result * 59 + ($bar == null ? 43 : $bar.hashCode());
return result;
}
public String toString() {
return "Example(foo=" + this.getFoo() + ", bar=" + this.getBar() + ")";
}
}
- @geter,@seter
例子-使用后:
public class Example{
@Getter @Setter
private int foo;
}
等价源码-使用前:
public int getFoo() {
return foo;
}
public void setFoo(int foo) {
this.foo= foo;
}
- @NonNull
实例-使用注解后:
import lombok.NonNull;
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();
}
}
- @SneakyThrows
实例:
@SneakyThrows(UnsupportedEncodingException.class)
public void utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
}
等价源码:
public void utf8ToString(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException $uniqueName) {
throw useMagicTrickeryToHideThisFromTheCompiler($uniqueName);
}
进一步了解认识注解的作用帮助我们进行开发,事半功倍;