Java中lombok的@Builder註解的解析與簡單使用詳解

Lombok中@Builder用法

1、建造者模式簡介:Builder 使用創建者模式又叫建造者模式。簡單來說,就是一步步創建一個對象,它對用戶屏蔽瞭裡面構建的細節,但卻可以精細地控制對象的構造過程。

2、註解類Builder.java註釋:

* The builder annotation creates a so-called ‘builder’ aspect to the class that is annotated or the class
 * that contains a member which is annotated with {@code @Builder}.
 * <p>
 * If a member is annotated, it must be either a constructor or a method. If a class is annotated,
 * then a private constructor is generated with all fields as arguments
 * (as if {@code @AllArgsConstructor(access = AccessLevel.PRIVATE)} is present
 * on the class), and it is as if this constructor has been annotated with {@code @Builder} instead.
 * Note that this constructor is only generated if you haven’t written any constructors and also haven’t
 * added any explicit {@code @XArgsConstructor} annotations. In those cases, lombok will assume an all-args
 * constructor is present and generate code that uses it; this means you’d get a compiler error if this
 * constructor is not present.

在企業開發中,一般在領域對象實體上標註@Builder,其作用就相當於@AllArgsConstructor(access = AccessLevel.PRIVATE),@Builder一般與@Getter結合使用。

3、實戰
① 編寫測試實體類。

import lombok.Builder;
import lombok.Getter;

@Builder
//@Getter
public class Person {
  private String name;

  private String id;

  private String phoneNumeber;
}

② 編寫測試類。

public class Test {
  public static void main(String[] args) {


    Person.PersonBuilder builder = Person.builder();
    builder.phoneNumeber("11111")
        .id("1123")
        .name("asdd").build();
    System.out.println(builder);


  }
}

③編譯並執行的結果為:
Person.PersonBuilder(name=asdd, id=1123, phoneNumeber=11111)

④ 編譯後的字節碼分析:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.atyunniao;

public class Person {
  private String name;
  private String id;
  private String phoneNumeber;

  Person(String name, String id, String phoneNumeber) {
    this.name = name;
    this.id = id;
    this.phoneNumeber = phoneNumeber;
  }

  public static Person.PersonBuilder builder() {
    return new Person.PersonBuilder();
  }

  public String getName() {
    return this.name;
  }

  public String getId() {
    return this.id;
  }

  public String getPhoneNumeber() {
    return this.phoneNumeber;
  }

  public static class PersonBuilder {
    private String name;
    private String id;
    private String phoneNumeber;

    PersonBuilder() {
    }

    public Person.PersonBuilder name(String name) {
      this.name = name;
      return this;
    }

    public Person.PersonBuilder id(String id) {
      this.id = id;
      return this;
    }

    public Person.PersonBuilder phoneNumeber(String phoneNumeber) {
      this.phoneNumeber = phoneNumeber;
      return this;
    }

    public Person build() {
      return new Person(this.name, this.id, this.phoneNumeber);
    }

    public String toString() {
      return "Person.PersonBuilder(name=" + this.name + ", id=" + this.id + ", phoneNumeber=" + this.phoneNumeber + ")";
    }
  }
}

@Builder的作用:
生成一個全屬性的構造器
生成瞭一個返回靜態內部類PersonBuilder對象的方法
生成瞭一個靜態內部類PersonBuilder,這個靜態內部類包含Person類的三個屬性,無參構造器,三個方法名為屬性名的方法,返回Person對象的build方法,輸出靜態內部類三個屬性的toString()方法。

⑤ 建造者使用過程:

 Person.PersonBuilder builder = Person.builder();
    builder.phoneNumeber("11111")
        .id("1123")
        .name("asdd").build();
    System.out.println(builder);

先實例化內部類對象並返回,然後為調用內部類的方法為內部類的屬性賦值,build()方法就是將內部類PersonBuilder的屬性值傳入Person構造器中,實例化Person對象。

以上即為對於@Builder的簡單使用。

到此這篇關於Java中lombok的@Builder註解的解析與簡單使用詳解的文章就介紹到這瞭,更多相關java lombok的@Builder註解內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: