您好,登录后才能下订单哦!
@Builder注解如何在Java中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
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中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。