lombok 插件使用时遇到的第2个坑

为了更简洁的构造bean对象,使用注解@Builder,然而发现,通过builder生成的bean对象,字段默认值没了(备注:日常开发中,bean 的成员变量尽量使用封装对象,以及尽量不要有默认值),但是通过new 得到的对象,字段默认值存在。

问题伪代码如下:

/** * @description: 用户父类 * @author: lyl * @create: 2022-06-01 14:42:27 **/@Datapublic class UserParent { /** * id */ private String id;}@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic class UserChildren extends UserParent{ /** * 姓名 */ private String name = “七夜”; /** * 年龄 */ private Integer age;}@SpringBootTestclass CodeTestApplicationTests {@Testvoid contextLoads() {UserChildren user1 = new UserChildren();System.out.println(user1);UserChildren user2 = UserChildren.builder().build();System.out.println(user2);}}执行结果如下:UserChildren(name=七夜, age=null)UserChildren(name=null, age=null)

针对lombok注解问题的排查,最简单的方式就是直接查看编译之后的代码。反编译后的UserCildren.class如下:

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by FernFlower decompiler)//package com.yalin.code.vo;public class UserChildren extends UserParent { private String name = “七夜”; private Integer age; public static UserChildrenBuilder builder() { return new UserChildrenBuilder(); } public String getName() { return this.name; } public Integer getAge() { return this.age; } public void setName(final String name) { this.name = name; } public void setAge(final Integer age) { this.age = age; } public boolean equals(final Object o) { if (o == this) { return true; } else if (!(o instanceof UserChildren)) { return false; } else { UserChildren other = (UserChildren)o; if (!other.canEqual(this)) { return false; } else { Object this$age = this.getAge(); Object other$age = other.getAge(); if (this$age == null) { if (other$age != null) { return false; } } else if (!this$age.equals(other$age)) { return false; } Object this$name = this.getName(); Object other$name = other.getName(); if (this$name == null) { if (other$name != null) { return false; } } else if (!this$name.equals(other$name)) { return false; } return true; } } } protected boolean canEqual(final Object other) { return other instanceof UserChildren; } public int hashCode() { int PRIME = true; int result = 1; Object $age = this.getAge(); result = result * 59 + ($age == null ? 43 : $age.hashCode()); Object $name = this.getName(); result = result * 59 + ($name == null ? 43 : $name.hashCode()); return result; } public String toString() { String var10000 = this.getName(); return “UserChildren(name=” + var10000 + “, age=” + this.getAge() + “)”; } public UserChildren() { } public UserChildren(final String name, final Integer age) { this.name = name; this.age = age; } public static class UserChildrenBuilder { private String name; private Integer age; UserChildrenBuilder() { } public UserChildrenBuilder name(final String name) { this.name = name; return this; } public UserChildrenBuilder age(final Integer age) { this.age = age; return this; } public UserChildren build() { return new UserChildren(this.name, this.age); } public String toString() { return “UserChildren.UserChildrenBuilder(name=” + this.name + “, age=” + this.age + “)”; } }}

通过查看编译之后的代码,可以看到,使用@Builder注解之后,lombok会生成一个UserChildrenBuilder的静态部类,这个类包含了UserChildren的成员变量,但是包含的成员变量中,name字段的初始值没了,当我们使用UserChildren.builder().build()来构造bean时,代码内部先生成一个UserChildrenBuilder的对象,然后对这个对象进行赋值,最后调用UserChildren的全参构造函数,生成UserChildren对象。就像一个代理一样!

因此:

使用new 对象时,没有使用到UserChildrenBuilder,因此name字段的初始值保留了。

使用builder构造对象时,UserChildrenBuilder的name字段没有了初始值,生成的对象,name字段自然就没值了。

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月12日
下一篇 2022年6月12日

相关推荐

  • Spring Boot 项目统一结果,统一异常,统一日志,写的太好了

    统一结果返回 目前的前后端开发大部分数据的传输格式都是json,因此定义一个统一规范的数据格式有利于前后端的交互与UI的展示。 统一结果的一般形式 是否响应成功; 响应状态码; 状…

    2022年6月15日
  • YG公司否认iKON成员BOBBY不续约:还在商议之中

    据韩媒报道称, 韩国YG娱乐公司今天正式否认了iKON成员BOBBY不再续约的传闻,称一切还没有确定,还在商议之中。 近日有多家海外媒体爆料,YG娱乐公司未能与iKON成员BOBB…

    2022年11月17日
  • 阿里云一面:详解下Spring MVC 框架

    一、Spring MVC 是什么 Spring MVC 全称 “Spring Web MVC” 是一个 基于 Servlet API 构建的原始 Web 框架 ,从一开始就包含在 …

    2022年6月25日
  • 基于数据驱动的接口自动化框架封装

    每天进步一点点,关注我们哦,每天分享测试技术文章 本文章出自【码同学软件测试】 码同学公众号:自动化软件测试 码同学抖音号:小码哥聊软件测试 1.数据驱动框架设计 1.框架结构 c…

    2022年6月19日
  • matplotlib 厘米和英寸的实现方法

    长度计量单位中的厘米和英寸是Python数据可视化中的常用计量单位,而且厘米和英寸可以相互换算。这样,在同一绘图区域中,同时使用厘米和英寸作为计量单位就成为可能。在代码实现上,厘米…

    2022年6月14日
  • 易烊千玺长发胡茬造型,诠释少年般沧桑,却尽显成熟魅力

    易烊千玺这位优秀的男孩,大多数都喜欢他这个人。他不仅长得帅气,而且特别成熟懂事。混迹在娱乐圈中,凭着自己的实力走红。出生于2000年的易烊千玺,是很多人心目中的偶像。 小小年纪却如…

    2022年6月19日
  • C++运算符重载为成员函数和非成员函数的限制

    我们知道,基本数据类型的定义包括: ① 值域或需要的内存空间; ② 位模式与编码方案; ③ 可以适用的操作(能够使用的运算符); 例如double类型,使用8个字节的内存空间,使用…

    2022年6月16日
  • C++|宽窄基本类型转换和父类子类转换

    基本数据类型有不同的编码方案(encoding scheme),需要不同的字节长度来适应各自的值域。 基本类型的宽窄转换,是一个整体的转换,其由窄到宽是安全的,由宽到窄可能会丢失数…

    2022年6月13日
  • 手机“wifi”明明已连接,却还是上不了网?教你3招,轻松搞定

    本文编辑今日头条作者维权骑士签约用户:小俊技术分享独家原创制作 未经授权严禁转载,发现抄袭者将进行全网维权投诉 分享生活小妙招,享受科技新生活!大家好,欢迎来到今天的知识分享!我是…

    2022年8月6日
  • wordpress使用the_excerpt函数显示文章的摘要信息

    如果你的文章需要摘要信息可以使用此函数,使用此函数需要了解如下四点1、它自动提取文章内容前55个字符作为文章摘要2、摘要不含有任何html和图片标签3、此函数只能用在循环中4、文章…

    2022年6月15日

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息