最全 Lombok介绍、使用方法和总结

1 Lombok背景介绍官方介绍如下:
Project Lombok makes JAVA a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code.大致意思是Lombok通过增加一些“处理程序” , 可以让java变得简洁、快速 。
2 Lombok使用方法Lombok能以简单的注解形式来简化java代码 , 提高开发人员的开发效率 。例如开发中经常需要写的javabean , 都需要花时间去添加相应的getter/setter , 也许还要去写构造器、equals等方法 , 而且需要维护 , 当属性多时会出现大量的getter/setter方法 , 这些显得很冗长也没有太多技术含量 , 一旦修改属性 , 就容易出现忘记修改对应方法的失误 。
Lombok能通过注解的方式 , 在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法 。出现的神奇就是在源码中没有getter和setter方法 , 但是在编译生成的字节码文件中有getter和setter方法 。这样就省去了手动重建这些代码的麻烦 , 使代码看起来更简洁些 。
Lombok的使用跟引用jar包一样 , 可以在官网(https://projectlombok.org/download)下载jar包 , 也可以使用maven添加依赖:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> <scope>provided</scope></dependency>接下来我们来分析Lombok中注解的具体用法 。
2.1 @Data
@Data注解在类上 , 会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法 , 如为final属性 , 则不会为该属性生成setter方法 。
官方实例如下:
 
import lombok.AccessLevel;import lombok.Setter;import lombok.Data;import lombok.ToString;@Data public class DataExample { private final String name; @Setter(AccessLevel.PACKAGE) private int age; private double score; private String[] tags;@ToString(includeFieldNames=true) @Data(staticConstructor="of") public static class Exercise<T> { private final String name; private final T value; }} 
如不使用Lombok , 则实现如下:
 
import java.util.Arrays;public class DataExample { private final String name; private int age; private double score; private String[] tags;public DataExample(String name) { this.name = name; }public String getName() { return this.name; }void setAge(int age) { this.age = age; }public int getAge() { return this.age; }public void setScore(double score) { this.score = score; }public double getScore() { return this.score; }public String[] getTags() { return this.tags; }public void setTags(String[] tags) { this.tags = tags; }@Override public String toString() { return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")"; }protected boolean canEqual(Object other) { return other instanceof DataExample; }@Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof DataExample)) return false; DataExample other = (DataExample) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false; if (this.getAge() != other.getAge()) return false; if (Double.compare(this.getScore(), other.getScore()) != 0) return false; if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false; return true; }@Override public int hashCode() { final int PRIME = 59; int result = 1; final long temp1 = Double.doubleToLongBits(this.getScore()); result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode()); result = (result*PRIME) + this.getAge(); result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32)); result = (result*PRIME) + Arrays.deepHashCode(this.getTags()); return result; }public static class Exercise<T> { private final String name; private final T value;private Exercise(String name, T value) { this.name = name; this.value = https://www.isolves.com/it/cxkf/yy/JAVA/2019-12-06/value; }public static Exercise of(String name, T value) { return new Exercise(name, value); }public String getName() { return this.name; }public T getValue() { return this.value; }@Override public String toString() { return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")"; }protected boolean canEqual(Object other) { return other instanceof Exercise; }@Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Exercise)) return false; Exercise other = (Exercise) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false; if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false; return true; }@Override public int hashCode() { final int PRIME = 59; int result = 1; result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode()); result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode()); return result; } }}


推荐阅读