用Lombok消除Java代码枯燥吧

Lombok是什么

Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO)。它通过注解实现这一目的。

如何下载

  1. 到官网下载jar包并运行,选取你所用的eclipse并install/update
    https://projectlombok.org/download
    这里写图片描述

  2. 或者用meven引入依赖

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
    </dependency>
</dependencies>

使用教程

https://projectlombok.org/features/

下面是整理的教程,供大家观赏


@Data简单粗暴,或者@Getter@Setter,搞定一切setget

如果觉得@Data这个注解有点简单粗暴的话,Lombok提供一些更精细的注解,比如@Getter,@Setter,(这两个是field注解),@ToString,@AllArgsConstructor(这两个是类注解).这些可能是最常见的用法。

  • @NoArgsConstructor: 自动生成无参数构造函数。
  • @AllArgsConstructor: 自动生成全参数构造函数。
  • @Data: 自动为所有字段添加@ToString, @EqualsAndHashCode, @Getter方法,为非final字段添加@Setter,和 @RequiredArgsConstructor!
public class User{
    private String username;
    private String password;
    //原本要生成一大堆setget
}
//相当于
@Data
public class User{
    private String username;
    private String password;
}
//相当于
@Getter
@Setter
public class User{
    private String username;
    private String password;
}

//然后Lombok就帮我们设置好了所有setget方法,是不是方便快捷

@NonNull: 可以帮助我们避免空指针

import lombok.NonNull;
    public class NonNullExample extends Something {
        private String name;  
        public NonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
    }
}
//相当于
public class NonNullExample extends Something {
    private String name;  
    public NonNullExample(@NonNull Person person) {
        super("Hello");
        if (person == null) {
            throw new NullPointerException("person");
        }
        this.name = person.getName();
    }
}

@Cleanup,自动释放

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);
    }
  }
}

//相当于

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();
      }
    }
  }
}

@ToString,自动重写ToString方法,但是还没搞懂

import lombok.Data;
import lombok.ToString;

@ToString(exclude="id")
@Data
public class LombokTest {
    private String width;
    private String height;
    private String id;
    private Square shape = new Square(5, 10);  

    @ToString(callSuper=true, includeFieldNames=true)  
      public static class Square{  
        private final int width, height;  
        public Square(int width, int height) {  
          this.width = width;  
          this.height = height;  
        }  
      }  

    public static void main(String[] args) {
        LombokTest object=new LombokTest();
        object.id="xxx";
        object.height="222px";
        object.width="333px";
        System.out.println(object.toString());
        System.out.println(object.shape.toString());
        System.out.println(object);
        System.out.println(object.shape);
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页