Lombok插件的安装与使用

Lombok是一个Java工具,通过注解减少冗余代码。本文介绍了Lombok的安装方法(IDEA和Eclipse),列举了常用的注解,并通过实例展示了如何在实际开发中应用Lombok,简化getter、setter、构造器等方法的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Lombok介绍:

Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法,省去我们手动创建这些代码的麻烦。

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

Lombok的安装:

IDEA安装Lombok插件

1.点击右上角的 File -> setting -> Plugins ,点击Browse repositories按钮,在搜索框输入Lombok Plugin进行搜索安装。

2.安装完成后,重启IDEA:

Eclipse安装Lombok插件:

1.去官网下载Lombok.jar 下载地址

2.下载后 双击运行lombok.jar(或使用命令java -jar xxx)运行时它开始会扫描你电脑上的IDE 扫描后如下图

当你看见这个图 表示安装成功

Lombok 注解

Lombok 常用的注解:

                      注解                                                                         描述
           @Getter / @Setter

可以作用在类上和属性上,放在类上,会对所有的非静态(non-static)属性生成Getter/Setter方法,放在属性上,会对该属性生成Getter/Setter方法。并可以使用该注解中的AccessLevel属性来指定Getter/Setter方法的访问级别

                  @ToString

生成toString方法,默认情况下,会输出类名、所有属性,属性会按照顺序输出,以逗号分割。可以使用该注解中的exclude属性来指定生成的toSpring方法不包含对象中的哪些字段,或者使用of属性来指定生成的toSpring方法只包含对象中的哪些字段

       @EqualsAndHashCode默认情况下,会使用所有非瞬态(non-transient)和非静态(non-static)字段来生成equals和hascode方法,也可以使用exclude或of属性。
        @NoArgsConstructor

生成无参构造器

     @RequiredArgsConstructor

会生成一个包含标识了@NonNull注解的变量的构造方法。生成的构造方法是private,如果想要对外提供使用的话,可以使用staticName选项生成一个static方法。

          @AllArgsConstructor

生成全参构造器

                      @Slf4j

该注解是用来解决不用每次都写 private final Logger logger = LoggerFactory.getLogger(XXX.class); 这句代码的。使用的日志框架是LogBack

                     @Log4j

该注解也是用来解决不用每次都写日志对象声明语句的,从字面上也可以看出,使用的日志框架是log4j

                     @Data

该注解是 @ToString、@EqualsAndHashCode注解,和所有属性的@Getter注解, 以及所有non-final属性的@Setter注解的组合,通常情况下,我们使用这个注解就足够了。

以上只列出了部分常用注解,更多注解的使用方式,请参考官网关于注解的文档

Lombok实战:

这里使用一个 Category 类来做为演示的例子,在使用Lombok之前,这个类里是写了getter setter方法以及构造函数的。现在我们使用Lombok将代码改造如下:

从右边的大纲中可以看出lombok根据标注的注解帮我们自动生成了相应的方法

编译后生成的代码如下,使用反编译工具进行查看:

package org.lht.entity;

import java.beans.ConstructorProperties;
import java.util.Date;

public class Category {
    private Integer id;
    private Integer parentId;
    private String name;
    private Boolean status;
    private Integer sortOrder;
    private Date createTime;
    private Date updateTime;

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

    public Integer getParentId() {
        return this.parentId;
    }

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

    public Boolean getStatus() {
        return this.status;
    }

    public Integer getSortOrder() {
        return this.sortOrder;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public Date getUpdateTime() {
        return this.updateTime;
    }

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

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

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

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Category() {
    }

    @ConstructorProperties({"id", "parentId", "name", "status", "sortOrder", "createTime", "updateTime"})
    public Category(Integer id, Integer parentId, String name, Boolean status, Integer sortOrder, Date createTime, Date updateTime) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
        this.status = status;
        this.sortOrder = sortOrder;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }

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

                return true;
            }
        }
    }

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

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

    public String toString() {
        return "Category(id=" + this.getId() + ", parentId=" + this.getParentId() + ", name=" + this.getName() + ", status=" + this.getStatus() + ", sortOrder=" + this.getSortOrder() + ", createTime=" + this.getCreateTime() + ")";
    }
}

如上,从反编译后的代码可以看到,getter setter方法和无参、全参构造器以及equals、hashcode、toString方法都生成出来了。在@EqualsAndHashCode注解中我们使用of属性指定只对比对象中id这个字段,所以生成的equals和hashcode只使用id这个字段作为因子,默认不指定的情况下是使用对象中所有的字段作为因子。而在@ToString注解中,我们使用exclude属性指定updateTime这字段不被输出,所以Lombok生成的toString方法中没有包含updateTime这个字段。

本博文参考于51CTO博客作者ZeroOne01 链接 特在次鸣谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值