Lombok插件的简介「建议收藏」

2022-10-04 20:23:34 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

Lombok插件的简介

  • 1 Lombok引入
  • 2 Lombok常见注解使用
    • 1 @Data
    • 2 @AllArgsConstructor
    • 3 @NoArgsConstructor
    • 4 @ToString
    • 5 @EqualsAndHashCode
    • 6 @Getter/@Setter
    • 7 @Slf4j
    • 8 @NonNull
    • 9 @Value
    • 10 @Builder
    • 11 @Synchronized
    • 12 @SneakyThrows
    • 13 @Cleanup
    • 14 @Accessors
  • 3 Lombok原理
  • 4 关于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:

代码语言:javascript复制
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:

代码语言:javascript复制
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class DepartMent { 

private String name;
}  

总结:

lombok的出现,是为了简化Java的Bean对象,将大量重复,没有技术意义的代码省略,不仅能使代码整洁美观,还能人们将注意力放到更重要的业务代码中.

使用前准备:

1 在IDEA中plugins中安装Lombok插件,重启IDEA.

2 添加lombok坐标

代码语言:javascript复制
                <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>

2 Lombok常见注解使用

1 @Data

@Data注解,自动生成对象属性的getter方法,setter方法,equals方法,hashCode方法,toString方法,无参构造方法.

厉害的童鞋提醒滴

代码语言:javascript复制
@Data
public class DepartMent { 

private String name;
}  

等同于:

代码语言:javascript复制
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注解, 对象的全参数构造

代码语言:javascript复制
@NoArgsConstructor
public class DepartMent { 

private String name;
}  

等同于:

代码语言:javascript复制
public class DepartMent { 

private String name;
public DepartMent(String name) { 

this.name = name;
}
}

3 @NoArgsConstructor

@NoArgsConstructor注解,对象的无参构造

代码语言:javascript复制
@NoArgsConstructor
public class DepartMent { 

private String name;
}  

等同于:

代码语言:javascript复制
public class DepartMent { 

private String name;
public DepartMent() { 
}
}

4 @ToString

@ToString注解,对象的toString方法

代码语言:javascript复制
@ToString
public class DepartMent { 

private String name;
}  

等同于:

代码语言:javascript复制
public class DepartMent { 

private String name;
@Override
public String toString() { 

return "DepartMent{"  
"name='"   name   '''  
'}';
}
}  

5 @EqualsAndHashCode

@EqualsAndHashCode注解,对象的equals和hashcode方法.

代码语言:javascript复制
@EqualsAndHashCode
public class DepartMent { 

private String name;
}  

等同于:

代码语言:javascript复制
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方法

代码语言:javascript复制
@Getter
@Setter  
public class DepartMent { 

private String name;
} 

等同于:

代码语言:javascript复制
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);

代码语言:javascript复制
@Slf4j 
public class DepartMent { 

private String name;
public static void main(String[] args) { 

log.info("日志: {}"   "Hello World!");
}
} 

等同于:

代码语言:javascript复制
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注解, 对象的成员变量和参数,标识不能为空,否则抛出空指针异常.

代码语言:javascript复制
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方法.

代码语言:javascript复制
@Value
public class DepartMent { 

private String name;
} 

等同于:

代码语言:javascript复制
public final class DepartMent { 

private final String name;
} 

10 @Builder

@Builder注解, 给类添加构造者模式

代码语言:javascript复制
@Builder
public class DepartMent { 

private String name;
} 

等同于:

代码语言:javascript复制
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注解,加一个同步锁

代码语言:javascript复制
public class DepartMent { 

private String name;
//普通方法,相当于对象锁
@Synchronized
void before() { 

//代码逻辑
}
//静态方法,相当于类锁
@Synchronized
static void after() { 

//代码逻辑
}   
} 

等同于:

代码语言:javascript复制
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 捕获异常.

代码语言:javascript复制
public class DepartMent { 

private String name;
@SneakyThrows
public void init(){ 

int a = 1 / 0;
}
} 

等同于:

代码语言:javascript复制
public class DepartMent { 

private String name;
public void init(){ 

try{ 

int a = 1 / 0;
}catch(Exception e){ 

//处理
}
}
} 

13 @Cleanup

@Cleanup注解,自动关闭资源,例如IO流对象.

代码语言:javascript复制
public class DepartMent { 

private String name;
public void init(){ 

@Cleanup InputStream in = new InputStream(); 
}
}

等同于:

代码语言:javascript复制
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 )

代码语言:javascript复制
@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 )

代码语言:javascript复制
@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方法会忽视属性名前的前缀.(剩下按照驼峰命名规则)

代码语言:javascript复制
@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依赖.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/196068.html原文链接:https://javaforall.cn

0 人点赞