在springboot中开启

默认包含了lombok包,只需开启就好了…

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

常用注解

@NonNull

给方法参数添加这个注解会自动在方法内对该参数进行是否为空的校验,如果为空,则会抛出空指针异常(NullPointerException)

测试代码

1
2
3
4
public static String nonNull(@NonNull String one) {
StringBuilder stringBuilder = new StringBuilder(one);
return stringBuilder.reverse().toString();
}

class文件

1
2
3
4
5
6
7
8
public static String reverse(@NonNull String one) {
if (one == null) {
throw new NullPointerException("one is marked @NonNull but is null");
} else {
StringBuilder stringBuilder = new StringBuilder(one);
return stringBuilder.reverse().toString();
}
}

2.@Cleanup

自动管理资源,用在局部变量之前,在当前变量范围内即将执行完毕退出之前会自动清理资源,自动生成try-finally这样的代码块来关闭流

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
public static void copyFile() throws Exception {
@Cleanup InputStream in = new FileInputStream("D:/MyTest/File/a.txt");
@Cleanup OutputStream out = new FileOutputStream("D:/MyTest/File/b.txt");
byte[] b = new byte[1024];
while (true) {
int l = in.read(b);
if (l == -1) {
break;
}
out.write(b, 0, l);
}
}

class文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void copyFile() throws Exception {
FileInputStream in = new FileInputStream("D:/MyTest/File/a.txt");
try {
FileOutputStream out = new FileOutputStream("D:/MyTest/File/b.txt");
try {
byte[] b = new byte[1024];
while(true) {
int l = in.read(b);
if (l == -1) {
return;
}
out.write(b, 0, l);
}
} finally {
if (Collections.singletonList(out).get(0) != null) {
out.close();
}
}
} finally {
if (Collections.singletonList(in).get(0) != null) {
in.close();
}
}
}

@Getter/@Setter

用在属性上,再也不用自己手写setter和getter方法了,还可以指定访问范围

测试代码

1
2
3
@Getter
@Setter
private String name;

class文件

1
2
3
4
5
6
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}

@ToString

用在类上,可以自动覆写toString方法,当然还可以加其他参数,例如@ToString(exclude=”id”)排除id属性,或者@ToString(callSuper=true, includeFieldNames=true)调用父类的toString方法,包含所有属性

测试代码

1
2
@ToString
public class LombokTest {

class文件

1
2
3
public String toString() {
return "LombokTest(name=" + this.getName() + ")";
}

@EqualsAndHashCode

用在类上,自动生成equals方法和hashCode方法

测试代码

1
2
@EqualsAndHashCode
public class LombokTest {

class文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof LombokTest)) {
return false;
} else {
LombokTest other = (LombokTest)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;
}
return true;
}
}
}

protected boolean canEqual(Object other) {
return other instanceof LombokTest;
}

public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}

@NoArgsConstructor、@RequiredArgsConstructor和@AllArgsConstructor

用在类上,自动生成无参构造和使用所有参数的构造函数以及把所有@NonNull属性作为参数的构造函数,如果指定staticName = “of”参数,同时还会生成一个返回类对象的静态工厂方法,比使用构造函数方便很多

测试代码

1
2
@AllArgsConstructor
public class LombokTest {

class文件

1
2
3
public LombokTest(String name) {
this.name = name;
}

@Data

注解在类上,相当于同时使用了@ToString、@EqualsAndHashCode、@Getter、@Setter和@RequiredArgsConstrutor这些注解,对于POJO类十分有用

@Value

用在类上,是@Data的不可变形式,相当于为属性添加final声明,只提供getter方法,而不提供setter方法

@Builder

用在类、构造器、方法上,为你提供复杂的builder APIs,让你可以像如下方式一样调用Person.builder().name(“Adam Savage”).city(“San Francisco”).job(“Mythbusters”).job(“Unchained Reaction”).build();更多说明参考Builder

@SneakyThrows

自动抛受检异常,而无需显式在方法上使用throws语句

测试代码

1
2
3
4
@SneakyThrows
public static void copyFile() {
@Cleanup InputStream in = new FileInputStream("D:/MyTest/File/a.txt");
}

class文件

1
2
3
4
5
6
7
8
9
10
public static void copyFile() {
try {
InputStream in = new FileInputStream("D:/MyTest/File/a.txt");
if (Collections.singletonList(in).get(0) != null) {
in.close();
}
} catch (Throwable var1) {
throw var1;
}
}

@Synchronized

用在方法上,将方法声明为同步的,并自动加锁,而锁对象是一个私有的属性$lock或$LOCK,而java中的synchronized关键字锁对象是this,锁在this或者自己的类对象上存在副作用,就是你不能阻止非受控代码去锁this或者类对象,这可能会导致竞争条件或者其它线程错误

测试代码

1
2
3
@Synchronized
public static void copyFile() {
}

class文件

1
2
3
4
5
6
public static void copyFile() {
Object var0 = $LOCK;
synchronized($LOCK) {
;
}
}

@Log

根据不同的注解生成不同类型的log对象,但是实例名称都是log,有六种可选实现类

  1. @CommonsLog
  2. @Log
  3. @Log4j
  4. @Log4j2
  5. @Slf4j
  6. @XSlf4j