最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Lombok的@Builder注解帶來的兩大坑

 更新時(shí)間:2022年08月27日 11:31:02   作者:Apple_Web  
這篇文章主要介紹了使用Lombok的@Builder注解帶來的兩大坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、@Data和@Builder導(dǎo)致無參構(gòu)造丟失

  • 單獨(dú)使用@Data注解,是會(huì)生成無參數(shù)構(gòu)造方法。
  • 單獨(dú)使用@Builder注解,發(fā)現(xiàn)生成了全屬性的構(gòu)造方法。

@Data和@Builder一起用:我們發(fā)現(xiàn)沒有了默認(rèn)的構(gòu)造方法。如果手動(dòng)添加無參數(shù)構(gòu)造方法或者用@NoArgsConstructor注解都會(huì)報(bào)錯(cuò)!

兩種解決方法

1、構(gòu)造方法加上@Tolerate 注解,讓lombok假裝它不存在(不感知)。

@Builder
@Data
public class TestLombok {
? ? @Tolerate
? ? TestLombok() {
? ? }
? ? ......
} ? ?

2、直接加上這4個(gè)注解

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {
? ? ......
} ? ?

二、@Builder注解導(dǎo)致默認(rèn)值無效

使用Lombok注解可以極高的簡化代碼量,比較好用的注解除了@Data之外,還有@Builder這個(gè)注解,它可以讓你很方便的使用builder模式構(gòu)建對象,但是今天發(fā)現(xiàn)@Builder注解會(huì)把對象的默認(rèn)值清掉。

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {
? ? private String aa = "zzzz";
? ? public static void main(String[] args) {
? ? ? ? TestLombok build = TestLombok.builder().build();
? ? ? ? System.out.println(build);
? ? }
}

輸出:

TestLombok(aa=null)

解決: 只需要在字段上面加上@Builder.Default注解即可

@Builder.Default
private String aa = "zzzz";

三、分析原因

我們使用注解的方式,底層本質(zhì)就是反射幫我們生成了一系列的setter、getter,所以我們直接打開編譯后的target包下面的.class文件,上面的所有原因一目了然!

源文件:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {
? ? private String aa = "zzzz";
? ? public static void main(String[] args) {
? ? ? ? TestLombok build = TestLombok.builder().build();
? ? ? ? System.out.println(build);
? ? }
}

對應(yīng)的class字節(jié)碼:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.apple.ucar;
public class TestLombok {
? ? private String aa = "zzzz";
? ? public static void main(String[] args) {
? ? ? ? TestLombok build = builder().build();
? ? ? ? System.out.println(build);
? ? }
? ? public static TestLombok.TestLombokBuilder builder() {
? ? ? ? return new TestLombok.TestLombokBuilder();
? ? }
? ? public String getAa() {
? ? ? ? return this.aa;
? ? }
? ? public void setAa(String aa) {
? ? ? ? this.aa = aa;
? ? }
? ? public boolean equals(Object o) {
? ? ? ? if (o == this) {
? ? ? ? ? ? return true;
? ? ? ? } else if (!(o instanceof TestLombok)) {
? ? ? ? ? ? return false;
? ? ? ? } else {
? ? ? ? ? ? TestLombok other = (TestLombok)o;
? ? ? ? ? ? if (!other.canEqual(this)) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? Object this$aa = this.getAa();
? ? ? ? ? ? ? ? Object other$aa = other.getAa();
? ? ? ? ? ? ? ? if (this$aa == null) {
? ? ? ? ? ? ? ? ? ? if (other$aa != null) {
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } else if (!this$aa.equals(other$aa)) {
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? protected boolean canEqual(Object other) {
? ? ? ? return other instanceof TestLombok;
? ? }
? ? public int hashCode() {
? ? ? ? int PRIME = true;
? ? ? ? int result = 1;
? ? ? ? Object $aa = this.getAa();
? ? ? ? int result = result * 59 + ($aa == null ? 43 : $aa.hashCode());
? ? ? ? return result;
? ? }
? ? public String toString() {
? ? ? ? return "TestLombok(aa=" + this.getAa() + ")";
? ? }
? ? public TestLombok() {
? ? }
? ? public TestLombok(String aa) {
? ? ? ? this.aa = aa;
? ? }
? ? public static class TestLombokBuilder {
? ? ? ? private String aa;
? ? ? ? TestLombokBuilder() {
? ? ? ? }
? ? ? ? public TestLombok.TestLombokBuilder aa(String aa) {
? ? ? ? ? ? this.aa = aa;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? public TestLombok build() {
? ? ? ? ? ? return new TestLombok(this.aa);
? ? ? ? }
? ? ? ? public String toString() {
? ? ? ? ? ? return "TestLombok.TestLombokBuilder(aa=" + this.aa + ")";
? ? ? ? }
? ? }
}

我們想知道@Data、@Builder等注解底層到底做了什么,直接編譯當(dāng)前文件,即可在生成的.class字節(jié)碼文件查看具體代碼便知道了

比如上述第二點(diǎn),采用@Builder的時(shí)候,這個(gè)aa并沒有默認(rèn)值,所以會(huì)為空??!

? public TestLombok.TestLombokBuilder aa(String aa) {
? ? ? ? ? ? this.aa = aa;
? ? ? ? ? ? return this;
? ? ? ? }

四、總結(jié)

個(gè)人覺得如果想要使用@Builder,最簡單的方法就是直接寫上這4個(gè)注解,有默認(rèn)值的話再加上@Builder.Default直接,正常情況下就沒啥問題了!

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {
?? ?@Builder.Default
? ? private String aa = "zzzz";
? ? public static void main(String[] args) {
? ? ? ? TestLombok build = TestLombok.builder().build();
? ? ? ? System.out.println(build);
? ? }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java設(shè)計(jì)模式之java組合模式詳解

    Java設(shè)計(jì)模式之java組合模式詳解

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之組合模式,簡單說明了組合模式的原理,并結(jié)合實(shí)例分析了java組合模式的具體用法,需要的朋友可以參考下
    2021-09-09
  • SpringBoot引入swagger報(bào)錯(cuò)處理的解決方法

    SpringBoot引入swagger報(bào)錯(cuò)處理的解決方法

    這篇文章主要給大家介紹SpringBoot引入swagger是會(huì)出現(xiàn)報(bào)錯(cuò)的處理解決方法,文中有詳細(xì)的解決過程,感興趣的小伙伴可以跟著小編一起來學(xué)習(xí)吧
    2023-06-06
  • 基于自定義校驗(yàn)注解(controller、method、(groups)分組的使用)

    基于自定義校驗(yàn)注解(controller、method、(groups)分組的使用)

    這篇文章主要介紹了基于自定義校驗(yàn)注解(controller、method、(groups)分組的使用),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java單元測試Powermockito和Mockito使用總結(jié)

    Java單元測試Powermockito和Mockito使用總結(jié)

    公司單元測試框架選用了Junit 4.12,Mock框架選用了Mockito和PowerMock,本文主要介紹了Java單元測試Powermockito和Mockito使用總結(jié),感興趣的可以了解一下
    2021-09-09
  • Java向上轉(zhuǎn)型和向下轉(zhuǎn)型的區(qū)別說明

    Java向上轉(zhuǎn)型和向下轉(zhuǎn)型的區(qū)別說明

    這篇文章主要介紹了Java向上轉(zhuǎn)型和向下轉(zhuǎn)型的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot之spring.factories的使用方式

    SpringBoot之spring.factories的使用方式

    這篇文章主要介紹了SpringBoot之spring.factories的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 親手教你SpringBoot中的多數(shù)據(jù)源集成問題

    親手教你SpringBoot中的多數(shù)據(jù)源集成問題

    本文主要是介紹基于springboot的多數(shù)據(jù)源切換,輕量級的一種集成方案,對于小型的應(yīng)用可以采用這種方案,我之前在項(xiàng)目中用到是因?yàn)楹唵危阌跀U(kuò)展以及優(yōu)化,對SpringBoot多數(shù)據(jù)源集成問題感興趣的朋友一起看看吧
    2022-03-03
  • Java中的輸出格式化問題小結(jié)

    Java中的輸出格式化問題小結(jié)

    在Java中,System.out.printf方法用于格式化輸出,格式化字符串`%.6f`表示浮點(diǎn)數(shù)保留6位小數(shù),其他格式化選項(xiàng)包括`%d`(整數(shù))、`%s`(字符串)和`%e`(科學(xué)計(jì)數(shù)法),示例代碼展示了如何使用這些格式化選項(xiàng),感興趣的朋友一起看看吧
    2025-02-02
  • java中的阻塞隊(duì)列應(yīng)用場景及代碼實(shí)例

    java中的阻塞隊(duì)列應(yīng)用場景及代碼實(shí)例

    這篇文章主要介紹了java中的阻塞隊(duì)列應(yīng)用場景及代碼實(shí)例阻塞隊(duì)列是一種特殊的隊(duì)列,它提供了線程安全的操作,并在隊(duì)列為空或滿時(shí)提供了阻塞的功能,阻塞隊(duì)列通常用于多線程場景,其中生產(chǎn)者線程向隊(duì)列中添加元素,而消費(fèi)者線程從隊(duì)列中獲取元素,需要的朋友可以參考下
    2024-01-01
  • 淺談Sharding-JDBC強(qiáng)制路由案例實(shí)戰(zhàn)

    淺談Sharding-JDBC強(qiáng)制路由案例實(shí)戰(zhàn)

    本文主要介紹了淺談Sharding-JDBC強(qiáng)制路由案例實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論

兰溪市| 临西县| 北海市| 垦利县| 台湾省| 蒙城县| 西安市| 高淳县| 郴州市| 天等县| 陆河县| 无棣县| 云梦县| 当阳市| 金坛市| 敦化市| 盘锦市| 自治县| 桂林市| 奉新县| 额敏县| 冀州市| 义马市| 兖州市| 巴马| 庆元县| 普兰店市| 彭州市| 宁武县| 赤水市| 延长县| 中超| 砀山县| 普安县| 武冈市| 沅陵县| 海门市| 长海县| 洪江市| 莒南县| 绥化市|