lombok的使用,简化代码又省力

逛博客的时候,经常遇到博主大力推荐使用lombok简化javabean代码,于是趁着新项目的开发,我就学习并应用了这个lombok,感觉用了lombok后就再也不想恢复到没有它的时候了。
这文章为自己学习lombok做个总结,便于回忆使用。

先看看一个典型的使用了lombok的javabean

@Data
public class User {
    private String id;
    private String name;
}

这个类同过使用注解@Data帮助省略显式的一些代码:所有属性的 getter 和 setter 方法,及equals、canEqual、hashCode、toString。

lombok是什么

lombok是一个省事的工具,通过用注解来帮我们省去手动创建一些必要的java样板方法(如:getter,setter),在编译代码时lombok会帮我们生成相关方法。

  • 官方地址:https://projectlombok.org/
  • github地址:https://github.com/rzwitserloot/lombok
  • jar:
    <dependency>
     <groupId>org.projectlombokgroupId>
     <artifactId>lombokartifactId>
     <version>1.16.14version>
    dependency>
  • ide需要安装lombok插件,不然调用用lombok省略的方法ide会提示错误。
    IntelliJ IDEA 下的话可以下载Lombok Plugin。

lombok注解

  • @Getter / @Setter
    用于类上,对所有的非静态属性生成Getter/Setter方法;
    用于属性上,对指定属性生成Getter/Setter方法;
    默认生成的Getter/Setter方法访问级别为public,可以通过类似这样的方式修改访问级别: @Setter(AccessLevel.PROTECTED) private String name;
  • @ToString
    用于生成toString方法,默认输出类名、所有属性,属性会按照顺序输出,以逗号分割;
    排除toString方法中输出属性,@ToString(exclude = "id")
    toString方法在调用父类toString,@ToString(callSuper = true)
  • @EqualsAndHashCode
    用于生成equals和hascode方法,默认 use all non-static, non-transient fields
    排除使用属性, @EqualsAndHashCode(exclude={"id", "shape"})
    使用父类属性, @EqualsAndHashCode(callSuper=true)
  • @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor
    构造方法,分别为无参构造器、部分参数构造器、全参构造器
    官方的列子:
With Lombok
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;

@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample {
  private int x, y;
  @NonNull private T description;

  @NoArgsConstructor
  public static class NoArgsExample {
    @NonNull private String field;
  }
}
Vanilla Java
public class ConstructorExample {
  private int x, y;
  @NonNull private T description;

  private ConstructorExample(T description) {
    if (description == null) throw new NullPointerException("description");
    this.description = description;
  }

  public static  ConstructorExample of(T description) {
    return new ConstructorExample(description);
  }

  @java.beans.ConstructorProperties({"x", "y", "description"})
  protected ConstructorExample(int x, int y, T description) {
    if (description == null) throw new NullPointerException("description");
    this.x = x;
    this.y = y;
    this.description = description;
  }

  public static class NoArgsExample {
    @NonNull private String field;

    public NoArgsExample() {
    }
  }
}
  • @Data
    @ToString,@EqualsAndHashCode,@Getter,@Setter和@RequiredArgsConstructor的功能集合,感觉这个比较常用。
  • @NonNull
    用于属性,局部变量,方法上,当对应参数为空时会抛出空指针异常。
  • @Builder
    用于生成一个构建器。
  • @Synchronized
    用于给指定方法加同步锁。
  • @Log
    省了一行获取log对象的代码。
@CommonsLog
    Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class); 
@JBossLog
    Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class); 
@Log
    Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); 
@Log4j
    Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class); 
@Log4j2
    Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class); 
@Slf4j
    Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class); 
@XSlf4j
    Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class); 

还有一些其他的注解可以在官网查看到。

lombok优缺点

  • 优点:省事,少些写很多模板代码;减少修改属性带来的错误。
  • 缺点: 可读性差;不支持多种参数构造器的重载。

你可能感兴趣的:(java工具类)