Lombok是什么
他的官网是这么写的
大概意思就是说:
Lombok项目很牛逼,使用它你就可以不用再写类似get/set或equals等方法,Lombok会帮你自动生成,还能自动帮你记录日志等等。
说的和真的一样,然而。。
引入Lombok
- Maven中引入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
- 同时在IDEA中安装Lombok插件
使用注解代替get/set方法
一个简单的pojo,之前都是command+n自动生成
public class student {
private long id; //学生id
private String name; //学生姓名
private String sex; //学生性别
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
使用Lombok,篇幅缩短一半
public class student {
@Getter
@Setter
private long id; //学生id
@Getter
@Setter
private String name; //学生姓名
@Getter
@Setter
private String sex; //学生性别
}
new一个对象
之前
Student student = new Student();
student.setId(1234);
student.setName("张三");
student.setSex("男");
使用Lombok,在pojo类上添加 @Builder
注解,然后
Student student = Student.builder().id(1234).name("张三").sex("男").build();
构造器
在pojo的类上使用@AllArgsConstructor
注解,相当于全参构造器
public Student(long id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
在pojo的类上使用@NoArgsConstructor
注解,相当于无参构造器
public Student() {
}
在pojo的类上使用@RequiredArgsConstructor
注解,相当于部分参构造器,但是需要在pojo的参数上添加final,比如
@RequiredArgsConstructor
public class Student {
private final long id; //学生id
private String name; //学生姓名
private String sex; //学生性别
}
相当于
public class Student {
private final long id; //学生id
private String name; //学生姓名
private String sex; //学生性别
public Student(long id) {
this.id = id;
}
}
空指针判断 @NonNull
public static void print(String data) {
if (data == null) {
System.out.println("传输的数据为空!!");
}
System.out.println(data);
}
相当于
public static void print(@NonNull String data) {
System.out.println(data);
}
资源泄漏 @Cleanup
之前需要访问资源后,必须关闭资源
BufferedReader br = null;
try {
String path = Main.class.getClassLoader().getResource("1.txt").getPath(); //获取文件路径
FileReader fileReader = new FileReader(path);
br = new BufferedReader(fileReader);
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); //这里必须关闭资源,并且try catch/抛异常
} catch (IOException e) {
e.printStackTrace();
}
}
使用Lombok后
BufferedReader br = null;
try {
String path = Main.class.getClassLoader().getResource("1.txt").getPath(); //获取文件路径
@Cleanup FileReader fileReader = new FileReader(path);
br = new BufferedReader(fileReader);
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
异常处理,@SneakyThrows
看到上面使用过@Cleanup的代码了吗,仍然需要try/catch,或者抛出异常;但是使用了@SneakyThrows注解后就会变成
@SneakyThrows
public static void main(String[] args) {
BufferedReader br = null;
String path = Main.class.getClassLoader().getResource("1.txt").getPath(); //获取文件路径
@Cleanup FileReader fileReader = new FileReader(path);
br = new BufferedReader(fileReader);
System.out.println(br.readLine());
}
但是建议你别用这个注解,因为当别人调用你的方法时无法显式地获取需要处理什么异常,小心被打
其他
除此之外还有很多注解,比如
- @Data:注在类上,提供类的get、set、equals、hashCode、canEqual、toString方法
- @ToString:为类自动生成toString()方法
- @EqualsAndHashCode:为类自动生成hashCode和equals实现
- @Log:为类自动生成log日志记录
- @Synchronized:为类方法或实例方法自动生成synchronized保护
结语
这些代码我一个快捷键就能自动生成,用这干嘛
Q.E.D.