作者:烧鸡太子爷

起源:恒生LIGHT云社区

后面介绍了一款java的maven插件叫maven helper,很多同学还挺感兴趣的,明天再接再厉,给大家再举荐一款还用的java插件叫lombok。先给大家讲讲Lombok的次要性能和如何应用,前面有工夫再讲讲Lombok的原理

Lombok是什么

先看段官网个的介绍阐明

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.

Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

lombok是一个能够通过简略的注解的模式来帮忙咱们简化打消一些必须有但显得很臃肿的 Java 代码的工具,简略来说,比方咱们新建了一个类,而后在其中写了几个字段,而后通常状况下咱们须要手动去建设getter和setter办法啊,构造函数啊之类的,lombok的作用就是为了省去咱们手动创立这些代码的麻烦,它可能在咱们编译源码的时候主动帮咱们生成这些办法

Lombok的次要性能

Lombok次要性能是通过注解的形式,用到的外围的性能我整顿画了一个图

Lombok的应用

官网阐明

官网有介绍很多引入的形式,能够参考官网install介绍

maven引入

本文只介绍maven引入,pom文件中间接引入lombok,目前官网最新的版本是1.18.22

<dependencies>    <dependency>        <groupId>org.projectlombok</groupId>        <artifactId>lombok</artifactId>        <version>1.18.22</version>        <scope>provided</scope>    </dependency></dependencies>

应用示例

lombok应用过程中次要是靠注解起作用的,官网上的文档外面有所有的注解,这里不一一列举,只拿@Data做举例,代码也是来自官网。

@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 = value;}public static <T> Exercise<T> of(String name, T value) {  return new Exercise<T>(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;}  }}

其余的能够参考官网的地址 https://projectlombok.org/fea...,点击进去就能够看到相干的阐明和应用样例

参考

官网:https://projectlombok.org/

文档:https://projectlombok.org/fea...

github我的项目:https://github.com/projectlom...