lombok 简化java代码注解

安装lombok插件
以intellij ide为例
File-->Setting-->Plugins-->搜索“lombok plugin”,安装后重启ide
lombok 注解
lombok 提供的注解不多,可以参考官方视频的讲解和官方文档。
Lombok 注解在线帮助文档:https://projectlombok.org/features/index.html
下面介绍几个我常用的 lombok 注解:
@Data:注解在类上;提供类所有属性的getting和setting方法,此外还提供了equals、canEqual、hashCode、toString方法
@Setter:注解在属性上;为属性提供setting方法
@Getter:注解在属性上;为属性提供getting方法
@Log4j:注解在类上;为类提供一个 属性名为log 的log4j日志对象
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
@NonNull:注解在参数上,可以省略重复的if( null == persion)这类异常处理
@Cleanup:注解在输入输出流等需要释放资源的变量上,不需要写额外繁琐而重复的释放资源代码
不使用lombok
import java.io.*;public class CleanupExample {
  public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    try {
      OutputStream out = new FileOutputStream(args[1]);
      try {
        byte[] b = new byte[10000];
        while (true) {
          int r = in.read(b);
          if (r == -1) break;
          out.write(b, 0, r);
        }
      } finally {
        if (out != null) {
          out.close();
        }
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }
}
使用lombok
import lombok.Cleanup;import java.io.*;
public class CleanupExample {
  public static void main(String[] args) throws IOException {
    @Cleanup InputStream in = new FileInputStream(args[0]);
    @Cleanup OutputStream out = new FileOutputStream(args[1]);
    byte[] b = new byte[10000];
    while (true) {
      int r = in.read(b);
      if (r == -1) break;
      out.write(b, 0, r);
    }
  }
}
val:最终局部变量,在迭代器循环时刻做简单缩写。
不使用lombok
public void example2() {  final HashMap<Integer, String> map = new HashMap<Integer, String>();
  map.put(0, "zero");
  map.put(5, "five");
  for (final Map.Entry<Integer, String> entry : map.entrySet()) {
    System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
  }
}
使用lombok
public void example2() { val map = new HashMap<Integer, String>();
 map.put(0, "zero");
 map.put(5, "five");
 for (val entry : map.entrySet()) {
   System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
 }
}
下面是简单示例
不使用 lombok 的方案
public class Person {    private String id;
    private String name;
    private String identity;
    private Logger log = Logger.getLogger(Person.class);
    public Person() {
    }
    public Person(String id, String name, String identity) {
        this.id              = id;
        this.name       = name;
        this.identity  = identity;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getIdentity() {
        return identity;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setIdentity(String identity) {
        this.identity = identity;
    }
}
使用 lombok 的方案
@Data@Log4j
@NoArgsConstructor
@AllArgsConstructor
public class Person {
    private String id;
    private String name;
    private String identity;
}
参考文档:
- https://projectlombok.org/features/index.html
 - http://www.blogjava.net/fancydeepin/archive/2012/07/12/lombok.html
 
以上是 lombok 简化java代码注解 的全部内容, 来源链接: utcz.com/z/391252.html

