大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
Lombok插件的简介
官网: https://projectlombok.org/
官方文档: https://projectlombok.org/api/lombok/package-summary.html
参考资料:
https://www.jianshu.com/p/2543c71a8e45
https://blog.csdn.net/zhaoyanjun6/article/details/80743302
1 Lombok引入
未使用lombok的Java的Bean:
public class DepartMent {
private String name;
public DepartMent() {
}
public DepartMent(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DepartMent that = (DepartMent) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
return "DepartMent{" +
"name='" + name + '\'' +
'}';
}
}
使用lombok的Java的Bean:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class DepartMent {
private String name;
}
总结:
lombok的出现,是为了简化Java的Bean对象,将大量重复,没有技术意义的代码省略,不仅能使代码整洁美观,还能人们将注意力放到更重要的业务代码中.
使用前准备:
1 在IDEA中plugins中安装Lombok插件,重启IDEA.
2 添加lombok坐标
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
2 Lombok常见注解使用
1 @Data
@Data
注解,自动生成对象属性的getter方法,setter方法,equals方法,hashCode方法,toString方法,无参构造方法.
厉害的童鞋提醒滴
@Data
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
public DepartMent() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DepartMent that = (DepartMent) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
2 @AllArgsConstructor
@AllArgsConstructor
注解, 对象的全参数构造
@NoArgsConstructor
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
public DepartMent(String name) {
this.name = name;
}
}
3 @NoArgsConstructor
@NoArgsConstructor
注解,对象的无参构造
@NoArgsConstructor
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
public DepartMent() {
}
}
4 @ToString
@ToString
注解,对象的toString方法
@ToString
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
@Override
public String toString() {
return "DepartMent{" +
"name='" + name + '\'' +
'}';
}
}
5 @EqualsAndHashCode
@EqualsAndHashCode
注解,对象的equals和hashcode方法.
@EqualsAndHashCode
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DepartMent that = (DepartMent) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
6 @Getter/@Setter
@Getter/@Setter
注解,对象的get/set方法
@Getter
@Setter
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
7 @Slf4j
@Slf4j
注解,日志打印,相当于public static final Logger log = LoggerFactory.getLogger(类名.class);
@Slf4j
public class DepartMent {
private String name;
public static void main(String[] args) {
log.info("日志: {}" + "Hello World!");
}
}
等同于:
public class DepartMent {
private String name;
public static final Logger log = LoggerFactory.getLogger(DepartMent.class);
public static void main(String[] args) {
log.info("日志: {}" + "Hello World!");
}
}
8 @NonNull
@NonNull
注解, 对象的成员变量和参数,标识不能为空,否则抛出空指针异常.
public class DepartMent {
@NonNull
private String name;
public String getName() {
return name;
}
public void setName(@NonNull String name) {
this.name = name;
}
}
9 @Value
@Value
注解, 把对象为final类,把成员变量默认定义private final
,且不生成set方法.
@Value
public class DepartMent {
private String name;
}
等同于:
public final class DepartMent {
private final String name;
}
10 @Builder
@Builder
注解, 给类添加构造者模式
@Builder
public class DepartMent {
private String name;
}
等同于:
public class DepartMent {
private String name;
DepartMent(String name) {
this.name = name;
}
public static DepartMent.DepartMentBuilder builder() {
return new DepartMent.DepartMentBuilder();
}
public static class DepartMentBuilder {
private String name;
DepartMentBuilder(){
};
public DepartMent.DepartMentBuilder name(String name) {
this.name = name;
return this;
}
public DepartMent build() {
return new DepartMent(this.name);
}
public String toString() {
return "DepartMent.DepartMentBuilder( name=" + this.name +")";
}
}
}
11 @Synchronized
@Synchronized
注解,加一个同步锁
public class DepartMent {
private String name;
//普通方法,相当于对象锁
@Synchronized
void before() {
//代码逻辑
}
//静态方法,相当于类锁
@Synchronized
static void after() {
//代码逻辑
}
}
等同于:
public class DepartMent {
private final Object $lock = new Object[0];
private static final Object $LOCK = new Object[0];
private String name;
void before() {
Object var1 = this.$lock;
synchronized(this.$lock) {
//代码逻辑
}
}
static void after() {
Object var0 = $LOCK;
synchronized($LOCK) {
//代码逻辑
}
}
}
12 @SneakyThrows
@SneakyThrows
注解, 等同于try/catch 捕获异常.
public class DepartMent {
private String name;
@SneakyThrows
public void init(){
int a = 1 / 0;
}
}
等同于:
public class DepartMent {
private String name;
public void init(){
try{
int a = 1 / 0;
}catch(Exception e){
//处理
}
}
}
13 @Cleanup
@Cleanup
注解,自动关闭资源,例如IO流对象.
public class DepartMent {
private String name;
public void init(){
@Cleanup InputStream in = new InputStream();
}
}
等同于:
public class DepartMent {
private String name;
public void init(){
try{
InputStream in = new InputStream();
}finally{
in.close();
}
}
14 @Accessors
@Accessors
注解,存取器,控制getter和setter方法的形式.注解有三个属性.
fluent
fluent默认为false,如果为true,则getter和setter方法的方法名都是属性名(如下name),且setter方法返回当前对象.(如下DepartMent )
@Data
@Accessors(fluent = true)
public class DepartMent {
private String name;
public String name() {
return name;
}
public DepartMent name(String name) {
//Xxx
}
}
chain
chain默认为false,如果为true,setter方法返回当前对象.(如下DepartMent )
@Data
@Accessors(chain = true)
public class DepartMent {
private String name;
public String getName() {
return name;
}
public DepartMent setName(String name) {
//Xxx
}
}
prefix
prefix默认为空的字符数组.可自定义前缀.(如下f),getter和setter方法会忽视属性名前的前缀.(剩下按照驼峰命名规则)
@Data
@Accessors(prefix = "f")
public class DepartMent {
private String fname;
public String name() {
return name;
}
public void name(String name) {
//Xxx
}
}
3 Lombok原理
jdk1.6,javac支持JSR 269 Pluggable Annotation Processing API
规范,程序实现该规范,在javac运行时,就可以调用.
Lombok就是实现了该规范的程序.在javac过程中的工作流程:
-
1 javac对源代码分析,生成一棵抽象语法树(AST)
-
2 javac编译过程调用Lombok程序
-
3 插件对得到的AST处理,找到注解地点,修改语法树,添加注解定义响应的树节点
-
4 javac将修改后的语法树(AST)生成字节码文件
4 关于Lombok总结
关于Lombok
的使用,众说纷纭,有人支持,有份反对. 新的技术出现,必定解决了一些生活中的问题,也必然带来新的问题.
Lombok
的使用,给代码带来了简洁美观, 节省大量基础代码.同时也带来了很多问题,如一人使用,全人必须使用;项目升级,新的框架能否使用该插件?
我相信Lombok
插件未来发展会越来越好.
tips:
IDEA2020.3
版本,已经将Lombok作为内置插件.SpringBoot的2.1.x
版本在Starter
中内置了Lombok依赖.
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/196068.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...