lombok的使用

一、如何使用

1. 添加依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>

2.配置插件

idea配置插件(不然会各种飘红)

二、常用注解

1.@Getter @Setter
使用范围: 1. 类上方 (所有属性自动注入) 2. 属性字段上方

public class LomBokTestEntiy {
    @Getter
    @Setter
    private String id;
    @Setter
    private String name;
}

查看对应的class文件

public class LomBokTestEntiy {
    private String id;
    private String name;

    public LomBokTestEntiy() {
    }
  //自动生成
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. @Data 聚合注解
    使用方式类上方,相当于 @ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor。
    此注解可以明显看出代码的简洁程度 ~
@Data
public class LomBokTestEntiy {

    private String id;

    private String name;
}

查看对应的LomBokTestEntiy.class文件

public class LomBokTestEntiy {
    private String id;
    private String name;

    public LomBokTestEntiy() {
    }

    public String getId() {
        return this.id;
    }

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

    public void setId(String id) {
        this.id = id;
    }

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

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof LomBokTestEntiy)) {
            return false;
        } else {
            LomBokTestEntiy other = (LomBokTestEntiy)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$id = this.getId();
                Object other$id = other.getId();
                if (this$id == null) {
                    if (other$id != null) {
                        return false;
                    }
                } else if (!this$id.equals(other$id)) {
                    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(Object other) {
        return other instanceof LomBokTestEntiy;
    }

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

    public String toString() {
        return "LomBokTestEntiy(id=" + this.getId() + ", name=" + this.getName() + ")";
    }
}
  1. @ToString 、 @EqualAndHashCode、 @NoArgsConstructor @RequiredArgsConstructor 、 @AllArgsConstructor
    自动生成toString(),equal(),hash(),无参构造,指定参数构造,全部参数构造。
    使用方式在class上方。
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class LomBokTestEntiy {

    private String id;
    private String name;
}
查看对应的class文件,注意一个有意思的事情,自动生成的方法顺序和注解的顺序保持一致。
public class LomBokTestEntiy {
    private String id;
    private String name;

    public String toString() {
        return "LomBokTestEntiy(id=" + this.getId() + ", name=" + this.getName() + ")";
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof LomBokTestEntiy)) {
            return false;
        } else {
            LomBokTestEntiy other = (LomBokTestEntiy)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$id = this.getId();
                Object other$id = other.getId();
                if (this$id == null) {
                    if (other$id != null) {
                        return false;
                    }
                } else if (!this$id.equals(other$id)) {
                    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(Object other) {
        return other instanceof LomBokTestEntiy;
    }

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

    public LomBokTestEntiy() {
    }

    public LomBokTestEntiy(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

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

    public String getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }
}
  1. @RequiredArgsConstructor @NonNull
    有参构造注解,在类上方。 非空注解可用于属性上方。
    备注: 有参构造函数,指定参数。 需要在对应的属性添加@NonNull注解。
@Setter
@Getter
//staticName 是创建一个静态builder方法,返回类对象.构造函数设置为private。
@RequiredArgsConstructor(staticName = "builder")

public class LomBokTestEntiy {
    public LomBokTestEntiy() {

    }

    @NonNull
    private String id;
    @NonNull
    private String name;
    private String age;
}


查看对应的class文件

import lombok.NonNull;

public class LomBokTestEntiy {
    @NonNull
    private String id;
    @NonNull
    private String name;
    private String age;

    public LomBokTestEntiy() {
    }

    public void setId(@NonNull String id) {
        if (id == null) {
            throw new NullPointerException("id is marked @NonNull but is null");
        } else {
            this.id = id;
        }
    }

    public void setName(@NonNull String name) {
        if (name == null) {
            throw new NullPointerException("name is marked @NonNull but is null");
        } else {
            this.name = name;
        }
    }

    public void setAge(String age) {
        this.age = age;
    }

    @NonNull
    public String getId() {
        return this.id;
    }

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

    public String getAge() {
        return this.age;
    }
   //注意不同
    private LomBokTestEntiy(@NonNull String id, @NonNull String name) {
        if (id == null) {
            throw new NullPointerException("id is marked @NonNull but is null");
        } else if (name == null) {
            throw new NullPointerException("name is marked @NonNull but is null");
        } else {
            this.id = id;
            this.name = name;
        }
    }
   // static 
    public static LomBokTestEntiy builder(@NonNull String id, @NonNull String name) {
        return new LomBokTestEntiy(id, name);
    }
}
  1. @Value
    使用位置: 类上方
    作用: 生成一个final类,类里面的属性以及方法 和@Data类似 。
@Value
public class TestValues {
    String age;
    String id;
}

对应的class

public final class TestValues {
    private final String age;
    private final String id;

    public TestValues(String age, String id) {
        this.age = age;
        this.id = id;
    }

    public String getAge() {
        return this.age;
    }

    public String getId() {
        return this.id;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof TestValues)) {
            return false;
        } else {
            TestValues other = (TestValues)o;
            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$id = this.getId();
            Object other$id = other.getId();
            if (this$id == null) {
                if (other$id != null) {
                    return false;
                }
            } else if (!this$id.equals(other$id)) {
                return false;
            }

            return true;
        }
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $age = this.getAge();
        int result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $id = this.getId();
        result = result * 59 + ($id == null ? 43 : $id.hashCode());
        return result;
    }

    public String toString() {
        return "TestValues(age=" + this.getAge() + ", id=" + this.getId() + ")";
    }
}

5、@Builder
生成一个内部builder类。可作用于类上方、方法、属性;
真正使用的场景还是在类上好理解。

@Builder
public class TestBuilder {
    private String id;
    private String name;
    @Builder.Default private long ts = System.currentTimeMillis();
}

对应的class文件

public class TestBuilder {
    private String id;
    private String name;
    private long ts;

    private static long $default$ts() {
        return System.currentTimeMillis();
    }

    TestBuilder(String id, String name, long ts) {
        this.id = id;
        this.name = name;
        this.ts = ts;
    }

    public static TestBuilder.TestBuilderBuilder builder() {
        return new TestBuilder.TestBuilderBuilder();
    }

    public static class TestBuilderBuilder {
        private String id;
        private String name;
        private boolean ts$set;
        private long ts;

        TestBuilderBuilder() {
        }

        public TestBuilder.TestBuilderBuilder id(String id) {
            this.id = id;
            return this;
        }

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

        public TestBuilder.TestBuilderBuilder ts(long ts) {
            this.ts = ts;
            this.ts$set = true;
            return this;
        }

        public TestBuilder build() {
            long ts = this.ts;
            if (!this.ts$set) {
                ts = TestBuilder.$default$ts();
            }

            return new TestBuilder(this.id, this.name, ts);
        }

        public String toString() {
            return "TestBuilder.TestBuilderBuilder(id=" + this.id + ", name=" + this.name + ", ts=" + this.ts + ")";
        }
    }
}


  1. @Log
    日志组件,可以生成一个log对象,直接使用。
    可作用于类&方法
@Slf4j
public class TestLog {
    public void  log(){
        log.info("test");
    }
}

对应的class文件

public class TestLog {
    private static final Logger log = LoggerFactory.getLogger(TestLog.class);

    public TestLog() {
    }

    public void log() {
        log.info("test");
    }
}

三、小结

1.lombok主要目的是代码简化,但是需要在易于review和简化之间找平衡点。

  1. lombok注解主要分为两大类,stable 和 experimental ,后者在后续版本有会删除风险。
  2. lombok用法很灵活,组合起来使用,会更好。
    附件:
    GitHub
    参考wiki
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容