美文网首页
lombok的使用

lombok的使用

作者: 木棍兒 | 来源:发表于2018-10-21 10:52 被阅读0次

在IDE中添加lombok插件

在idea中安装lombok插件


image.png image.png

在eclipse中安装lombok插件

下载lombok.jar包https://projectlombok.org/download.html

运行lombok.jar.png 安装到eclipse.png

在pom文件中添加依赖

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

lombok常用注解

  1. @Getter,@Setter。注解可以针对类的属性字段自动生成Get/Set方法。


    位于类上方.png
    位于属性上方.png

上面的写法相当于这样写


上面的写法相当于这样写.png

2.@NoArgsConstructor,@AllArgsConstructor。注解为类自动生成了无参构造方法、和包含所有参数的构造方法。

image.png

上面的写法相当于这样写


上面的写法相当于这样写.png

3.@Data。生成get,set,toString,equals,hashCode,canEqual方法


image.png

上面的写法相当于

public class Booth {
    private String name;
    private String remark;

    public Booth() {
    }

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

    public String getRemark() {
        return this.remark;
    }

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

    public void setRemark(String remark) {
        this.remark = remark;
    }

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

                Object this$remark = this.getRemark();
                Object other$remark = other.getRemark();
                if (this$remark == null) {
                    if (other$remark != null) {
                        return false;
                    }
                } else if (!this$remark.equals(other$remark)) {
                    return false;
                }

                return true;
            }
        }
    }

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

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

    public String toString() {
        return "Booth(name=" + this.getName() + ", remark=" + this.getRemark() + ")";
    }
}

4.@Builder。提供builder设计模式的实现。

@Builder
public class Booth {
    private String name;

    private String remark;
}

在获取Booth对象时

private Booth getBooth(String name, String remark){
        Booth booth = Booth.builder()
                .name(name)
                .remark(remark)
                .build();
        return booth;
    }

当使用@Builder时将不会自动生成无参构造方法(会生成一个全参数的构造方法)
@Builder,@NoArgsConstructor不能同时使用

原理

lombok工作原理 lombok工作流程

在编译过程中Source File(源代码文件)被转换成AST(抽象语法树)然后在注解处理过程中调用实现了JSR 269 API规范的lombok程序,lombok修改AST。修改后的AST会被解析并生成字节码文件。

相关文章

  • 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/lhtvzftx.html