中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

SpringBoot | 第二章:lombok 介紹及簡(jiǎn)單使用

2018-07-28    來(lái)源:importnew

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

在去北京培訓(xùn)的時(shí)候,講師說(shuō)到了lombok這個(gè)第三方插件包,使用了之后發(fā)現(xiàn),確實(shí)是個(gè)神奇,避免了編寫(xiě)很多臃腫的且定式的代碼,雖然現(xiàn)代的IDE都能通過(guò)快捷鍵或者右鍵的方式,使用Generate Getters and Setters快速生成setters/getters,但當(dāng)某一個(gè)字段修改或者添加字段時(shí),又需要重復(fù)的操作一遍,但使用了lombok之后。一切都是自動(dòng)的,除了最常用的生成setters/getters,還有諸如:自動(dòng)生成toString方法、equals、·haashcode·等,還能快速生成Builder模式的javabean類,實(shí)在是方便。程序猿是很懶的,一切重復(fù)的工作都想通過(guò)腳本或者自動(dòng)化工具來(lái)完成,所以,使用lombok吧。

為何要使用Lombok

我們?cè)陂_(kāi)發(fā)過(guò)程中,通常都會(huì)定義大量的JavaBean,然后通過(guò)IDE去生成其屬性的構(gòu)造器、getter、setter、equals、hashcode、toString方法,當(dāng)要增加屬性或者對(duì)某個(gè)屬性進(jìn)行改變時(shí),比如命名、類型等,都需要重新去生成上面提到的這些方法。這樣重復(fù)的勞動(dòng)沒(méi)有任何意義,Lombok里面的注解可以輕松解決這些問(wèn)題。

  • 簡(jiǎn)化冗余的JavaBean代碼,使得實(shí)體文件很簡(jiǎn)潔。
  • 大大提高JavaBean中方法的執(zhí)行效率,省去重復(fù)的步驟

Lombok簡(jiǎn)介

Lombok是一個(gè)可以通過(guò)簡(jiǎn)單的注解形式來(lái)幫助我們簡(jiǎn)化消除一些必須有但顯得很臃腫的Java代碼的工具,通過(guò)使用對(duì)應(yīng)的注解,可以在編譯源碼的時(shí)候生成對(duì)應(yīng)的方法。

官方地址:https://projectlombok.org/?github地址:https://github.com/rzwitserloot/lombok

官網(wǎng)對(duì)其解釋為:

這里簡(jiǎn)單說(shuō)下lombok實(shí)現(xiàn)的原理:主要是通過(guò)抽象語(yǔ)法樹(shù)(AST),在編譯處理后,匹配到有其注解的類,那么注解編譯器就會(huì)自動(dòng)去匹配項(xiàng)目中的注解對(duì)應(yīng)到在lombok語(yǔ)法樹(shù)中的注解文件,并經(jīng)過(guò)自動(dòng)編譯匹配來(lái)生成對(duì)應(yīng)類中的getter或者setter方法,達(dá)到簡(jiǎn)化代碼的目的。

利用此原理,也可自行編寫(xiě)一些工作中一些經(jīng)常使用到的,比如實(shí)體類轉(zhuǎn)Map對(duì)象,map對(duì)象轉(zhuǎn)實(shí)體類,原本使用Beanutils或者cglib的BeanCopier實(shí)現(xiàn)轉(zhuǎn)換,前者使用的是反射的機(jī)制,所以性能相對(duì)較差,后者是使用修改字節(jié)碼技術(shù),性能在未使用Converter時(shí)基本等同于setget方法。但說(shuō)白了還是麻煩,畢竟還需要緩存對(duì)象等做到復(fù)用等。而使用lombok的形式的話,一切都是自動(dòng)的,性能基本是沒(méi)有損失的,由于對(duì)AST不熟悉,之后有時(shí)間了可以進(jìn)行插件編寫(xiě)下(去官網(wǎng)提過(guò)這個(gè)問(wèn)題,官方回復(fù)說(shuō),不太符合lombok的使用場(chǎng)景,⊙﹏⊙‖∣,還是自己動(dòng)手,風(fēng)衣足食吧~)

eclipse 安裝

  1. 下載 lombok.jar 包
  2. 運(yùn)行lombok.jar包,會(huì)自動(dòng)掃描系統(tǒng)的ide安裝情況(或者手動(dòng)指定目錄),點(diǎn)擊Install/Update,即可。

?

  1. 不運(yùn)行jar包情況下,可直接指定eclipse.ini文件,設(shè)置javaagent屬性即可(第二種方法最后的效果也是這樣的。):

Lombok使用

添加maven依賴

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
</dependency>

常用注解介紹

  1. @Getter / @Setter:可以作用在類上和屬性上,放在類上,會(huì)對(duì)所有的非靜態(tài)(non-static)屬性生成Getter/Setter方法,放在屬性上,會(huì)對(duì)該屬性生成Getter/Setter方法。并可以指定Getter/Setter方法的訪問(wèn)級(jí)別。
  2. @EqualsAndHashCode?:默認(rèn)情況下,會(huì)使用所有非瞬態(tài)(non-transient)和非靜態(tài)(non-static)字段來(lái)生成equals和hascode方法,也可以指定具體使用哪些屬性。?@ToString?生成toString方法,默認(rèn)情況下,會(huì)輸出類名、所有屬性,屬性會(huì)按照順序輸出,以逗號(hào)分割。
  3. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor:無(wú)參構(gòu)造器、部分參數(shù)構(gòu)造器、全參構(gòu)造器
  4. ** @Data:包含@ToString, @EqualsAndHashCode, 所有屬性的@Getter, 所有non-final屬性的@Setter和@RequiredArgsConstructor的組合,通常情況下,基本上使用這個(gè)注解就足夠了。**
  5. @Budilder:可以進(jìn)行Builder方式初始化。
  6. @Slf4j:等同于:private final Logger logger = LoggerFactory.getLogger(XXX.class);簡(jiǎn)直不能更爽了!一般上用在其他java類上

更多注解說(shuō)明,可查看:https://projectlombok.org/features/index.html

簡(jiǎn)單使用示例

使用lombok

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Demo {

    String code;
    String name;

}

等同于

public class Demo {
        String code;
        String name;

        public static DemoBuilder builder() {
            return new DemoBuilder();
        }

        public String getCode() {
            return this.code;
        }

        public String getName() {
            return this.name;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean equals(Object o) {
            if (o == this)
                return true;
            if (!(o instanceof Demo))
                return false;
            Demo other = (Demo) o;
            if (!other.canEqual(this))
                return false;
            Object this$code = getCode();
            Object other$code = other.getCode();
            if (this$code == null ? other$code != null : !this$code.equals(other$code))
                return false;
            Object this$name = getName();
            Object other$name = other.getName();
            return this$name == null ? other$name == null : this$name.equals(other$name);
        }

        protected boolean canEqual(Object other) {
            return other instanceof Demo;
        }

        public int hashCode() {
            int PRIME = 59;
            int result = 1;
            Object $code = getCode();
            result = result * 59 + ($code == null ? 43 : $code.hashCode());
            Object $name = getName();
            return result * 59 + ($name == null ? 43 : $name.hashCode());
        }

        public String toString() {
            return "Demo(code=" + getCode() + ", name=" + getName() + ")";
        }

        public Demo() {
        }

        public Demo(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public static class DemoBuilder {
            private String code;
            private String name;

            public DemoBuilder code(String code) {
                this.code = code;
                return this;
            }

            public DemoBuilder name(String name) {
                this.name = name;
                return this;
            }

            public Demo build() {
                return new Demo(this.code, this.name);
            }

            public String toString() {
                return "Demo.DemoBuilder(code=" + this.code + ", name=" + this.name + ")";
            }
        }
    }

使用@Slf4j(摘抄至官網(wǎng))

@Slf4j
public class LogExampleOther {

  public static void main(String... args) {
    log.error("Something else is wrong here");
  }
}

常規(guī)的

public class LogExampleOther {
  private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);

  public static void main(String... args) {
    log.error("Something else is wrong here");
  }
}

省了多少事。。∩倌昕焓褂冒!

標(biāo)簽: 代碼 腳本

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:深入學(xué)習(xí) Java 線程池

下一篇:SpringBoot | 第一章:第一個(gè) SpringBoot 應(yīng)用