Lombok介绍
一、介绍
官方介绍:
Project Lombok makes java a spicier language by adding ‘handlers’ that know how to build and compile simple, boilerplate-free, not-quite-java code.
大致意思是Lombok通过增加一些“处理程序”,可以让java变得简洁、快速。
二、基本使用
-
Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。例如开发中经常需要写的javabean,都需要花时间去添加相应的getter/setter,也许还要去写构造器、equals等方法,而且需要维护,当属性多时会出现大量的getter/setter方法,这些显得很冗长也没有太多技术含量,一旦修改属性,就容易出现忘记修改对应方法的失误。
-
Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。出现的神奇就是在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法。这样就省去了手动重建这些代码的麻烦,使代码看起来更简洁些。
-
Lombok的使用跟引用jar包一样,可以在[官网]: ( https://projectlombok.org/download )下载jar包,也可以使用maven添加依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
- 在Idea中,选择Setting-plugins,在插件中心下载Lombok,否则会编译失败
2.1 Lombok的注解
2.2 @Setter/@Getter
使用@Getter/@Setter注解,此注解在属性上,可以为相应的属性自动生成Getter/Setter
但是如果该属性为final和static属性,则不会生成对应的方法
注意:这是因为final修饰为最终变量,不可用setter方法,静态变量属于类变量,不属于成员变量,也可以单独在成员变量上加上注解
2.3 @toString
public String toString() {
return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", email=" + this.getEmail() + ", username=" + this.getUsername() + ")";
}
User(id=1, name=ljt, age=18, email=ljt@qq.com, username=ljt)
自动生成的toString方法
@ToString(of = {"username","password"}) of强制指定字段
User(username=ljt, password=123)
@ToString(exclude = {"id","name"}) exclude排除字段
User(age=18, email=ljt@qq.com, username=ljt)
2.4 @EqualsAndHashCode
自动生成equals和hashcode方法
编译的文件
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else {
label71: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label71;
}
} else if (this$id.equals(other$id)) {
break label71;
}
return false;
}
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
label57: {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
break label57;
}
} else if (this$name.equals(other$name)) {
break label57;
}
return false;
}
Object this$email = this.getEmail();
Object other$email = other.getEmail();
if (this$email == null) {
if (other$email != null) {
return false;
}
} else if (!this$email.equals(other$email)) {
return false;
}
Object this$username = this.getUsername();
Object other$username = other.getUsername();
if (this$username == null) {
if (other$username == null) {
return true;
}
} else if (this$username.equals(other$username)) {
return true;
}
return false;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $email = this.getEmail();
result = result * 59 + ($email == null ? 43 : $email.hashCode());
Object $username = this.getUsername();
result = result * 59 + ($username == null ? 43 : $username.hashCode());
return result;
}
2.5 @NonNull
主要作用与形参上的,非空判断
public void setName(@NonNull String name) {
this.name = name;
}
//编译后:
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked non-null but is null");
} else {
this.name = name;
}
}
//测试空值
user.setName(null);
Exception in thread "main" java.lang.NullPointerException: name is marked non-null but is null
2.6 @NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor
作用与类上
@NoArgsConstructor 生成无参构造方法
@AllArgsConstructor 所有字段的构造方法 final和static不会加入
@RequiredArgsConstructor 针对NoNull和final类型生成的
Lombok没法实现多种参数构造器的重载。
2.7 @Data
@Data集合了@ToString、@EqualsAndHashCode、@Getter/@Setter、@RequiredArgsConstructor的所有特性
2.7 @Builder
该注解会生成一个Builder对象,通过UserBuilder对参数进行赋值,调用build生成user对象
User user = new UserBuilder().age(1).email("ljt@qq.com").name("ljt").id(1L).build();
System.out.println(user);
User(id=1, name=ljt, age=1, email=ljt@qq.com, username=ljt)
2.8 @Log
该注解可以生成一个log日志对象,方便日志使用
log.info("test");
二月 22, 2021 12:32:02 下午 com.ljt.mybatis.domain.User main
信息: test
2.9 val
不是注解,定义类型,了解即可
val map=new HashMap<String,String>();
2.10 @cleanup
该注解能帮助我们自动调用close()方法,很大的简化了代码,例如FileInputStream
@Cleanup FileInputStream fileInputStream = new FileInputStream("a.txt");
@Cleanup FileOutputStream fileOutputStream=new FileOutputStream("a.txt");
FileInputStream fileInputStream = new FileInputStream("a.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream("a.txt");
if (Collections.singletonList(fileOutputStream).get(0) != null) {
fileOutputStream.close();
}
} finally {
if (Collections.singletonList(fileInputStream).get(0) != null) {
fileInputStream.close();
}
}
eam).get(0) != null) {
fileOutputStream.close();
}
} finally {
if (Collections.singletonList(fileInputStream).get(0) != null) {
fileInputStream.close();
}
}