SpringBoot中Lombok使用
一、Lombok介绍
lombok是一个ide插件,它可以让我们写更少的代码,而编译出更多的字节码。
我们可以让我们编译出一个复杂的.class文件,而我们的.java文件很干净清爽。
lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过通用对应的注解,在编译源码的时候生成对应的方法。
二、常用注解
1 @Getter @Setter :分别为字段生成getter和setter,bool值的getter形如isField
@Getter @Setter private boolean employed = true;
@Setter(AccessLevel.PROTECTED) private String name;
等价于
private boolean employed = true;
private String name;
public boolean isEmployed() {
return employed;
}
public void setEmployed(final boolean employed) {
this.employed = employed;
}
protected void setName(final String name) {
this.name = name;
}
2 @NonNull:生成setter方法的字段上时,将生成一个null检查,如果提供null值,则将产生NullPointerException
@Getter @Setter @NonNull
private List<Person> members;
等价于
@NonNull
private List<Person> members;
public Family(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
@NonNull
public List<Person> getMembers() {
return members;
}
public void setMembers(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
3 @ToString:默认所有的非静态字段都会以键值对的方式输出,可以通过设置includeFieldNames=false不显示键,of设置包含的字段列表,exclude设置要排除的字段
@ToString(callSuper=true,exclude="someExcludedField")
public class Foo extends Bar {
private boolean someBoolean = true;
private String someStringField;
private float someExcludedField;
}
等价于
public class Foo extends Bar {
private boolean someBoolean = true;
private String someStringField;
private float someExcludedField;
@java.lang.Override
public java.lang.String toString() {
return "Foo(super=" + super.toString() +
", someBoolean=" + someBoolean +
", someStringField=" + someStringField + ")";
}
}
4 @EqualsAndHashCode:生成equals和hashCode方法,of设置包含的字段列表,exclude设置要排除的字段
@EqualsAndHashCode(callSuper=true,exclude={"address","city","state","zip"})
public class Person extends SentientBeing {
enum Gender { Male, Female }
@NonNull private String name;
@NonNull private Gender gender;
private String ssn;
private String address;
private String city;
private String state;
private String zip;
}
等价于
public class Person extends SentientBeing {
enum Gender {
/*public static final*/ Male /* = new Gender() */,
/*public static final*/ Female /* = new Gender() */;
}
@NonNull
private String name;
@NonNull
private Gender gender;
private String ssn;
private String address;
private String city;
private String state;
private String zip;
@java.lang.Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != this.getClass()) return false;
if (!super.equals(o)) return false;
final Person other = (Person)o;
if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
if (this.gender == null ? other.gender != null : !this.gender.equals(other.gender)) return false;
if (this.ssn == null ? other.ssn != null : !this.ssn.equals(other.ssn)) return false;
return true;
}
@java.lang.Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + super.hashCode();
result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
result = result * PRIME + (this.gender == null ? 0 : this.gender.hashCode());
result = result * PRIME + (this.ssn == null ? 0 : this.ssn.hashCode());
return result;
}
}
5.@Data:等价于使用@Getter,@Setter,@ToString,@@EqualsAndHashCode
6.@Slf4j,@Log4j,@Log4j2:类级别注解,生成私有静态可以使用的日志输出对象,在类中直接通过log对象调
三、使用
1.插件安装
2.maven依赖
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>