美文网首页
lombok的使用

lombok的使用

作者: 红色的飞猪 | 来源:发表于2018-12-21 15:11 被阅读0次

一、如何使用

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

相关文章

  • lombok

    lombok的安装以及使用 什么是lombok 有什么好处 如何使用 IntelliJ IDEA 安装lombok...

  • 安装lombok

    使用lombok要先安装! 使用lombok要先安装! 使用lombok要先安装! 编译jar包后会自己找到相关的...

  • lombok 插件使用技巧

    lombok @EqualsAndHashCode 注解的影响 Java中优雅的使用Lombok

  • 推荐几个自己使用的使用IDEA插件

    推荐几个自己使用的使用IDEA插件 Lombok plugin 介绍 使用Lombok时候可以安装这个插件,能够让...

  • Lombok的基本用法

    一、引入lombok IDEA还需要下载lombok插件。 二、lombok的使用 1.@Data @Data的功...

  • IDEA报: Lombok Requires Annotatio

    一、问题描述 IDEA使用lombok,提示: Lombok Requires Annotation Proces...

  • lombok guide

    lombok guide lombok是什么 lombok怎么用 这里不谈原理,不讲性能,只谈使用。 这里只说比较...

  • Jhipster之使用lombok工具

    关于lombok介绍,请参考lombok 在jhipster中使用lombok,在pom.xml中修改如下 1.加...

  • SpringBoot注解学习之@Accessors

    Accessor的中文含义是存取器;它是在lombok插件下使用的,使用前需下载lombok插件。 包含的属性: ...

  • 如何学习lombok

    如何学习lombok lombok为什么出现,解决了什么问题? 在项目中使用Lombok可以减少很多重复代码的书写...

网友评论

      本文标题:lombok的使用

      本文链接:https://www.haomeiwen.com/subject/srfykqtx.html