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注解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!