前言
最近,经同事介绍,知道一个非常有用的插件Lombok,仔细一查,已经有很多人开始使用Lombok技术了。
简介
Lombok最大的特点就是通过注释来简化程序员的代码,在编译器编译时通过操作AST(抽象语法树)改变字节码生成。
官网地址:https://projectlombok.org/
Git项目地址:https://github.com/rzwitserloot/lombok
注意:官网首页有一个4分钟左右的视频,简单介绍了Lombok的使用,有兴趣的可以直接看视频了解一下
使用
下面介绍几个个人觉得比较常用的Lombok注释:
1. @Data
普通实体类实现的方式
public class Entity{
private Long id;
private String name;
public Entity(){
super();
}
public Entity(Long id, String name){
this.id = id;
this.name = name;
}
public void setId(Long id){
this.id = id;
}
public Long getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
Lombok的实现方式
public @Data class Entity{
private Long id;
private String name;
}
@Data是一种利用注释来实现POJO类的方法他避免了程序员实现大量的getter/setter方法转而用注释替代。
它同时包含了:
@ToString、
@EqualsAndHashCode、
@Getter / @Setter、
@NoArgsConstructor/@AllArgsConstructor/@RequiredArgsConstructor的功能
只需要@Data一个注释,代码在编译时就会自动为我们实现toString方法、equals方法以及HashCode方法、getter/setter方法、无参构造、有参构造。
当然,我们也可以在使用的时候,更有针对性的配置。
@NoArgsConstructor// 实现无参构造
@AllArgsConstructor// 实现包含所有参数的构造
public class Entity{
// 默认的getter方法为public,access可以定义生成方法的修饰符
@Getter(access = AccessLevel.PROTECTED)
// 实现默认的setter方法
@Setter
private Long id;
@Getter
private String name;
}
相当于
public class Entity{
private Long id;
private String name;
// @NoArgsConstructor
public Entity(){
super();
}
// @AllArgsConstructor
public Entity(Long id, String name){
this.id = id;
this.name = name;
}
// @Setter
public void setId(Long id){
this.id = id;
}
// @Getter(access = AccessLevel.PROTECTED)
protected Long getId(){
return id;
}
// @Setter
public String getName(){
return name;
}
}
2. @Log
在Lombok中我们可以通过注释就使用日志对象,当然对应日志的包还是要引入的。
Lombok支持多种类型的日志
@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
之前在做项目的时候要调用Log就要在类中声明出来
public class Entity{
private static final Logger log = LoggerFactory.getLogger(Entity.class);
public void todo(){
log.error("...");
}
}
Lombok调用日志功能
@Slf4j
public class Entity{
public void todo(){
log.error("...");
}
}
3. @NunNull
@NunNull可以用于成员变量、方法变量的空指针验证,只要在变量前加上@NunNull注释则编译时会对变量
加上验证,变量为空则抛出NullPointException
public class Entity{
@NonNull
@Setter
private Long id;
public void toDo(@NonNull String something) {
}
}
编译后结果
public class Entity{
@NonNull
private Long id;
public void setId(@NonNull Long id)
{
if (id == null) {
throw new NullPointerException("id");
}
this.id = id;
}
public void toDo(@NonNull String something)
{
if (something == null) {
throw new NullPointerException("something");
}
}
}