Lombok的使用

7 篇文章 0 订阅

Lombok的使用

1,了解Lombok

看到网上的大佬都在使用Lombok插件我也就跟风学习一下。

首先,当然看的是官网。官网是这样解释的:

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

这里是说Lombok项目是一个会自动插入到你的编译器和构建工具中的Java库,从而是你的Java更加生动有趣。不需要再写任何的getter和equals方法,你的带有注解的有功能全面的生成器,能够自动化你的日志记录等

有关Lombok的学习点击

2,安装Lombok

如果用IDEA编辑器的最新版(2020.3)的话,会发现该编译器已经内置了Lombok插件,直接俄使用就可以。使用maven开发的话直接在pom.xml文件中添加依赖即可。

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
3,Lombok常用注解
  1. @Getter and @Setter 自动为类生成属性相对应的getXx()方法以及setXx()方法

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

在这里插入图片描述

  1. @Data注解:这个注解包含范围最广,使用该注解会自动生成一个无参构造函数,属性的get和set方法,toString方法,hashCode方法,equals等,如下:
import lombok.*;
@Data
public class User {
    private String name ;
    private int id;
}

在这里插入图片描述

  1. @NoArgsConstructor注解: 生成该类的无参构造方法

  2. @AllArgsConstructor注解 :生成对应的有参构造方法,这里需要注意的是,当同时使用了@data和@AllArgsConstructor注解时,此时是没有无参构造函数的,需要自行添加 @NoArgsConstructor注解。如下:

@Data
@AllArgsConstructor
public class User {
    private String name ;
    private int id;
}

在这里插入图片描述

  1. @ToString注解 :生成toString方法

  2. @EqualsAndHashCode注解:使用此注解会自动重写对应的equals方法和hashCode方法

  3. @NonNull注解: 使用该注解后便会自动对方法中该参数值进行判空。 为了方便理解此处直接上代码

import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    @NonNull private String name ;
    private int id;
}

相当于:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import lombok.NonNull;

public class User {
    @NonNull
    private String name;
    private int id;

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

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

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

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

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

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

                return true;
            }
        }
    }

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

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

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

4,优缺点

优点:

  1. 减少了大量的模板代码。
  2. 提高了代码的开发效率。

缺点:

  1. 当有n个属性时不能实现1~n-1个参数的重载
  2. 一定程度上降低了代码的可读性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值