在一个POJO的定义中,我们往往会用到很多setter和很多的getter,如果这些全部由我们自己手动敲出来,岂不是太浪费时间?
Lombok在这方面起到了很大的作用。
依赖
要使用Lombok,首先我们要将Lombok引入maven
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
安装插件
在IDEA中使用Lombok时仅仅引入时不够的,因为Lombok的生效是在编译阶段,而我们在编写的时候如果不安装插件则会因为代码检测不到相应的setter和getter而报错
插件安装方法:
File — setting – Plugins处搜索Lombok并且下载

开始使用
注解
@Data
@Setter
@Getter
@Log4j2
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@NonNull
@Cleanup
@ToString
@RequiredArgsConstructor
@Value
@SneakyThrows
@Synchronized
详解
为了比较区别,我使用java源码与编译后的class字节码文件反编译的代码进行比较
@Data
注解在类上;提供类所有属性的 get 和 set 方法,此外还提供了equals、canEqual、hashCode、toString 方法。
//HotNew.java
@Data
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class反编译
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew() {
}
public long getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public long getAuthorId() {
return this.authorId;
}
public void setId(final long id) {
this.id = id;
}
public void setTitle(final String title) {
this.title = title;
}
public void setAuthorId(final long authorId) {
this.authorId = authorId;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof HotNew)) {
return false;
} else {
HotNew other = (HotNew)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
return false;
} else {
Object this$title = this.getTitle();
Object other$title = other.getTitle();
if (this$title == null) {
if (other$title == null) {
return this.getAuthorId() == other.getAuthorId();
}
} else if (this$title.equals(other$title)) {
return this.getAuthorId() == other.getAuthorId();
}
return false;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof HotNew;
}
public int hashCode() {
int PRIME = true;
int result = 1;
long $id = this.getId();
int result = result * 59 + (int)($id >>> 32 ^ $id);
Object $title = this.getTitle();
result = result * 59 + ($title == null ? 43 : $title.hashCode());
long $authorId = this.getAuthorId();
result = result * 59 + (int)($authorId >>> 32 ^ $authorId);
return result;
}
public String toString() {
return "HotNew(id=" + this.getId() + ", title=" + this.getTitle() + ", authorId=" + this.getAuthorId() + ")";
}
}
@Setter
注解在类上,提供类中所有变量的setter; 注解在变量上, 提供单个变量的setter
//注解在类上: HotNew.java
@Setter
public class HotNew {
private long id;
private String title;
private long authorId;
}
//注解在类上: HotNew.class
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew() {
}
public void setId(final long id) {
this.id = id;
}
public void setTitle(final String title) {
this.title = title;
}
public void setAuthorId(final long authorId) {
this.authorId = authorId;
}
}
@Getter
同@Setter用法
//注解在类上: HotNew.java
@Getter
public class HotNew {
private long id;
private String title;
private long authorId;
}
//注解在类上: HotNew.class
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew() {
}
public long getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public long getAuthorId() {
return this.authorId;
}
}
@Log4j2
注解在类上,为类提供一个 属性名为 log 的 log4j2 日志对象,提供默认构造方法。
//HotNew.java
@Log4j2
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class
public class HotNew {
private static final Logger log = LogManager.getLogger(HotNew.class);
private long id;
private String title;
private long authorId;
public HotNew() {
}
}
@AllArgsConstructor
注解在类上,提供一个全参的构造方法,且会移除默认的无参构造方法
//HotNew.java
@AllArgsConstructor
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew(final long id, final String title, final long authorId) {
this.id = id;
this.title = title;
this.authorId = authorId;
}
}
@NoArgsConstructor
注解在类上,为类提供一个无参的构造方法
//HotNew.java
@NoArgsConstructor
@AllArgsConstructor
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew() {
}
public HotNew(final long id, final String title, final long authorId) {
this.id = id;
this.title = title;
this.authorId = authorId;
}
}
@EqualsAndHashCode
注解在类上, 可以生成 equals、canEqual、hashCode 方法。
//HotNew.java
@EqualsAndHashCode
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew() {
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof HotNew)) {
return false;
} else {
HotNew other = (HotNew)o;
if (!other.canEqual(this)) {
return false;
} else if (this.id != other.id) {
return false;
} else {
Object this$title = this.title;
Object other$title = other.title;
if (this$title == null) {
if (other$title == null) {
return this.authorId == other.authorId;
}
} else if (this$title.equals(other$title)) {
return this.authorId == other.authorId;
}
return false;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof HotNew;
}
public int hashCode() {
int PRIME = true;
int result = 1;
long $id = this.id;
int result = result * 59 + (int)($id >>> 32 ^ $id);
Object $title = this.title;
result = result * 59 + ($title == null ? 43 : $title.hashCode());
long $authorId = this.authorId;
result = result * 59 + (int)($authorId >>> 32 ^ $authorId);
return result;
}
}
@NonNull
注解在变量上,自动产生改参数的非空检查,参数为空则报空指针
//HotNew.java
public class HotNew {
@NonNull
private long id;
private String title;
private long authorId;
}
//HotNew.class
public class HotNew {
@NonNull
private long id;
private String title;
private long authorId;
public HotNew() {
}
}
@Cleanup
这个注解用在变量前面,可以保证此变量代表的资源会被自动关闭,默认是调用资源的 close() 方法,如果有其它关闭方法,可使用 @Cleanup(“方法名”) 来指定要调用的方法
//Test.java
public class Test {
public static void main(String[] args) throws IOException {
@Cleanup InputStream inputStream = new FileInputStream(args[0]);
}
}
//Test.class
public class Test {
public Test() {
}
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(args[0]);
if (Collections.singletonList(inputStream).get(0) != null) {
inputStream.close();
}
}
}
@ToString
这个注解用在类上,生成所有参数的 toString 方法
//HotNew.java
@ToString
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class
public class HotNew {
private long id;
private String title;
private long authorId;
public HotNew() {
}
public String toString() {
return "HotNew(id=" + this.id + ", title=" + this.title + ", authorId=" + this.authorId + ")";
}
}
@RequiredArgsConstructor
注解到类上,产生类中所有带@NonNull和final修饰的成员变量的构造方法
//HotNew.java
@RequiredArgsConstructor
public class HotNew {
private long id;
@NonNull
private String title;
private final long authorId;
}
//HotNew.class
public class HotNew {
private long id;
@NonNull
private String title;
private final long authorId;
public HotNew(@NonNull final String title, final long authorId) {
if (title == null) {
throw new NullPointerException("title is marked non-null but is null");
} else {
this.title = title;
this.authorId = authorId;
}
}
}
@Value
注解在类上,产生全参的构造方法,get方法,equals、hasCode、toString方法
//HotNew.java
@Value
public class HotNew {
private long id;
private String title;
private long authorId;
}
//HotNew.class
public final class HotNew {
private final long id;
private final String title;
private final long authorId;
public HotNew(final long id, final String title, final long authorId) {
this.id = id;
this.title = title;
this.authorId = authorId;
}
public long getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public long getAuthorId() {
return this.authorId;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof HotNew)) {
return false;
} else {
HotNew other = (HotNew)o;
if (this.getId() != other.getId()) {
return false;
} else {
Object this$title = this.getTitle();
Object other$title = other.getTitle();
if (this$title == null) {
if (other$title == null) {
return this.getAuthorId() == other.getAuthorId();
}
} else if (this$title.equals(other$title)) {
return this.getAuthorId() == other.getAuthorId();
}
return false;
}
}
}
public int hashCode() {
int PRIME = true;
int result = 1;
long $id = this.getId();
int result = result * 59 + (int)($id >>> 32 ^ $id);
Object $title = this.getTitle();
result = result * 59 + ($title == null ? 43 : $title.hashCode());
long $authorId = this.getAuthorId();
result = result * 59 + (int)($authorId >>> 32 ^ $authorId);
return result;
}
public String toString() {
return "HotNew(id=" + this.getId() + ", title=" + this.getTitle() + ", authorId=" + this.getAuthorId() + ")";
}
}
@SneakyThrows
注解在方法上,可以将方法中的代码用 try-catch 语句包裹起来,捕获异常并在 catch 中用 把异常抛出,可以使用 @SneakyThrows(Exception.class) 的形式指定抛出哪种异常,也会生成默认的构造方法。
//Test.java
public class Test {
@SneakyThrows(IOException.class)
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(args[0]);
}
}
//Test.class
public class Test {
public Test() {
}
public static void main(String[] args) throws IOException {
try {
new FileInputStream(args[0]);
} catch (IOException var2) {
throw var2;
}
}
}
@Synchronized
这个注解用在类方法或者实例方法 上,效果和 synchronized 关键字相同synchronized 关键字的锁对象分别是类的 class 对象和 this 对象,而 @Synchronized 的锁对象默认是私有静态 final 对象 lock 和 私有 final 对象 lock,当然,也可以自己指定锁对象,此外也提供默认的构造方法。
无参考代码
9625

被折叠的 条评论
为什么被折叠?



