当前位置:网站首页>Lombok的一些使用心得
Lombok的一些使用心得
2022-08-04 05:27:00 【何亚告】
Lombok的一些使用心得
Lombok官网
地址:https://projectlombok.org/
官网简介:Project Lombok是一个Java库,可以自动插入到您的编辑器中并构建工具,为您的Java增添趣味。 再也不用再写另一个
getter 或 equals 方法了,只要有一个注释,你的类就有了一个功能齐全的生成器、自动化你的日志记录变量等等。
Lombok的一些fetures
使用心得
在SpringBoot中引入Lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
@Data
首先是最常用的@Data,使用方式往往就是在类上注释,它会自动引入:
[email protected]和@Setter
[email protected]
[email protected]
[email protected]
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private String name;
private Integer age;
}
编译后打开class可以看到添加了@Data注解后的效果:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public String getName() {
return this.name;
}
public Integer getAge() {
return this.age;
}
public void setName(final String name) {
this.name = name;
}
public void setAge(final Integer age) {
this.age = age;
}
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 {
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;
}
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;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
return result;
}
public String toString() {
return "User(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
/*这个构造函数是@AllArgsConstructor生成的*/
public User(final String name, final Integer age) {
this.name = name;
this.age = age;
}
}
@NoArgsConstructor, @RequiredArgsConstructor 和@AllArgsConstructor
在使用的过程中,对这三个注解的效果有一些没分清,所以各使用了一次查看了编译后的代码:
@NoArgsConstructor:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public User() {
}
}
@RequiredArgsConstructor:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public User() {
}
}
@AllArgsConstructor:
package com.hgh.boot.bean;
public class User {
private String name;
private Integer age;
public User(final String name, final Integer age) {
this.name = name;
this.age = age;
}
}
可见@NoArgsConstructor和@RequiredArgsConstructor在编译后生成的构造函数是一样的,那区别在哪里呢?
经过查找资料:@NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor的区别以及在springboot常用地方
@NoArgsConstructor:生成无参的构造方法。
@AllArgsConstructor:生成该类下全部属性的构造方法。
@RequiredArgsConstructor:生成该类下被final修饰或者non-null字段生成一个构造方法。
也就是说@RequiredArgsConstructor只会构造有final或者no-null修饰的字段
当我对User类的属性进行final或non-null修饰时,@NoArgsConstructor和@RequiredArgsConstructor构造出来的构造函数才会有区别:
package com.hgh.boot.bean;
public class User {
private final String name;
private Integer age;
public User(final String name) {
this.name = name;
}
}
边栏推荐
猜你喜欢
随机推荐
lambda函数用法总结
(Kettle) pdi-ce-8.2 连接MySQL8.x数据库时驱动问题之终极探讨及解决方法分析
登录页面js手写
MySQL date functions
Vulnhub:Sar-1
js 基础学习笔记
Swoole学习(二)
The cost of automated testing is high and the effect is poor, so what is the significance of automated testing?
webrtc中视频采集实现分析(一) 采集及图像处理接口封装
LCP 17. Quick Calculation Robot
箭头函数的使用
7.16 Day22---MYSQL (Dao mode encapsulates JDBC)
Sublime Text 3 2021.8.3 个人配置
自动化运维工具Ansible(5)流程控制
7.13 Day20----MYSQL
基于C语言的学生信息管理系统_(更新版)_(附源码和安装包)_课程设计_**往事随風**的博客
4.1 JdbcTemplate for declarative transactions
TensorRTx-YOLOv5工程解读(一)
TensorRT例程解读之语义分割demo
BUUCTF——MISC(一)