亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

      
      

            <dl id="hur0q"><div id="hur0q"></div></dl>

                lombok 插件使用時遇到的第2個坑

                為了更簡潔的構(gòu)造bean對象,使用注解@Builder,然而發(fā)現(xiàn),通過builder生成的bean對象,字段默認值沒了(備注:日常開發(fā)中,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);}}執(zhí)行結(jié)果如下: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的靜態(tài)內(nèi)部類,這個類包含了UserChildren的成員變量,但是包含的成員變量中,name字段的初始值沒了,當(dāng)我們使用UserChildren.builder().build()來構(gòu)造bean時,代碼內(nèi)部先生成一個UserChildrenBuilder的對象,然后對這個對象進行賦值,最后調(diào)用UserChildren的全參構(gòu)造函數(shù),生成UserChildren對象。就像一個代理一樣!

                因此:

                使用new 對象時,沒有使用到UserChildrenBuilder,因此name字段的初始值保留了。

                使用builder構(gòu)造對象時,UserChildrenBuilder的name字段沒有了初始值,生成的對象,name字段自然就沒值了。

                鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系管理員(admin#wlmqw.com)刪除。
                用戶投稿
                上一篇 2022年6月12日 14:30
                下一篇 2022年6月12日 14:31

                相關(guān)推薦

                • 存儲過程語法(sql server存儲過程語法)

                  今天小編給各位分享存儲過程語法的知識,其中也會對sql server存儲過程語法進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧! oracle存儲過程基本語法…

                  2022年11月26日
                • 馬斯克凌晨一點半曬“代碼審查”現(xiàn)場,編排他的段子比瘋狂星期四還多

                  夢晨 Pine 發(fā)自 凹非寺 量子位 | 公眾號 QbitAI 每一個真正會寫代碼的人,請在下午2點到總部10層報到。 每一個真正會寫代碼的人,請在下午2點到總部10層報到。 馬斯…

                  2022年11月21日
                • 微信群沒有減號怎么踢人(微信群聊怎么踢人)

                  我們在創(chuàng)建群之后,最頭疼的問題就是有人在群里發(fā)垃圾營銷廣告鏈接了。但是我們有時候經(jīng)常會因為手頭上剛好有事情在做不能及時踢掉這些人而導(dǎo)致社群人員流失。 現(xiàn)在企業(yè)微信有了防騷擾功能,我…

                  2022年11月18日
                • 曝iKON成員BOBBY續(xù)約失敗 將離開YG尋找新東家

                  據(jù)韓媒方面的消息表示,男子團體iKON 近日與YG合約到期,正在進行續(xù)約的討論,但是成員BOBBY疑似續(xù)約失敗,將要離開YG。 據(jù)業(yè)內(nèi)知情人士表示,iKON 成員BOBBY續(xù)約失敗…

                  2022年11月17日
                • YG公司否認iKON成員BOBBY不續(xù)約:還在商議之中

                  據(jù)韓媒報道稱, 韓國YG娛樂公司今天正式否認了iKON成員BOBBY不再續(xù)約的傳聞,稱一切還沒有確定,還在商議之中。 近日有多家海外媒體爆料,YG娛樂公司未能與iKON成員BOBB…

                  2022年11月17日
                • 網(wǎng)站客服代碼(網(wǎng)站客服代碼實現(xiàn)移動端隱藏,電腦端展開)

                  本文主要講的是網(wǎng)站客服代碼,以及和網(wǎng)站客服代碼實現(xiàn)移動端隱藏,電腦端展開相關(guān)的知識,如果覺得本文對您有所幫助,不要忘了將本文分享給朋友。 在線客服系統(tǒng)代碼是什么? 在線客服系統(tǒng)代碼…

                  2022年11月12日
                • 8字頭股票什么意思(8字頭股票什么意思呀)

                  北京證券交易所股票是以4和8開頭1北京證券交易所是以現(xiàn)有的新三板精選層為基礎(chǔ)組建,進一步提升服務(wù)中小企業(yè)的能力,打造服務(wù)創(chuàng)新型中小企業(yè)主陣地北京證券交易所是因為我們國家要支持中小企…

                  2022年11月11日
                • 錦州疫情什么時候可以恢復(fù)正常(錦州疫情什么時候開始的)

                  最近一段時間錦州也出現(xiàn)了疫情,為了疫情防控,當(dāng)?shù)貙嵭袑嵭徐o態(tài)管理,大家都想知道解封時間,那么,錦州什么時候解封恢復(fù)正常出行?錦州疫情預(yù)計什么時候結(jié)束解封?下面小編為大家?guī)礤\州疫情…

                  2022年11月9日
                • 昆明經(jīng)開區(qū)洛羊街道什么時候解封(昆明經(jīng)開區(qū)洛羊街道屬于哪個區(qū))

                  近日云南省昆明疫情新增大家也都有在持續(xù)關(guān)注,為了能夠盡快控制疫情,昆明針對一些疫情區(qū)域?qū)嵭袊?yán)格的管控措施,其中昆明經(jīng)開區(qū)洛羊街道從11月8日起部分區(qū)域?qū)嵭信R時靜態(tài)管理,昆明經(jīng)開區(qū)洛…

                  2022年11月8日
                • 現(xiàn)在平遙能離開嗎?今日出入平遙最新通知

                  作為山西旅游大縣,近日平遙古城檢出陽性人員,如今平遙全縣都實行了靜默管理。11月7日晚,平遙縣舉行新冠肺炎疫情防控新聞發(fā)布會,截至目前,平遙縣共發(fā)現(xiàn)27例陽性感染者?,F(xiàn)在平遙還滯留…

                  2022年11月8日

                聯(lián)系我們

                聯(lián)系郵箱:admin#wlmqw.com
                工作時間:周一至周五,10:30-18:30,節(jié)假日休息