亲宝软件园·资讯

展开

java lombok的@Builder注解 Java中lombok的@Builder注解的解析与简单使用详解

VirtuousGuy 人气:0

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的简单使用。

加载全部内容

相关教程
猜你喜欢
用户评论