Lombok使用总结

目录

一、Lombok简介

二、IntelliJ IDEA 使用它的方法

三、Lombok注解

 @Getter/@Setter

@ToString

@EqualsAndHashCode

@NonNull

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

@Data

 @Accessors

@Synchronized 

@Builder

@Log

@Wither


一、Lombok简介

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

官方地址:https://projectlombok.org/

github地址:https://github.com/rzwitserloot/lombok

二、IntelliJ IDEA 使用它的方法

  • 先安装插件

  • 然后引入lombok的jar包
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.14</version>
</dependency>

三、Lombok注解

 @Getter/@Setter

1、可以为单个成员变量设置get方法 

2、可以为所有成员变量设置get、set方法 。同时可以为某个成员变量设置其他权限(默认public)、或设置取消get/set方法

3. 无法为static设置get/set方法,只为final类型设置get方法

@ToString

1、ToString只能加在类上,自动生成ToString方法,使用exclude排除多个字段,of必须包含哪些字段。

 

 2、如果继承的有父类的话,可以设置callSuper 让其调用父类的toString()方法

@ToString(callSuper = true)

@EqualsAndHashCode

生成equals方法、canEqual、hashCode方法。也可以进行相等比较的排除,指定。

@EqualsAndHashCode(exclude = {"字段1","字段2"})


    public boolean equals(final 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 {
                label47: {
                    Object this$id = this.id;
                    Object other$id = other.id;
                    if (this$id == null) {
                        if (other$id == null) {
                            break label47;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label47;
                    }

                    return false;
                }

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

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

                return true;
            }
        }
    }

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

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

@NonNull

可以加在成员变量前,也可以加在方法参数前。给方法参数增加这个注解自动在方法内对该参数进行是否为空的检验,如果为空,则抛出NPE(NullPointerException)

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

@NoArgsConstructor生成一个无参构造方法。当类中有final字段没有被初始化时,编译器会报错,此时可用@NoArgsConstructor(force = true),然后就会为没有初始化的final字段设置默认值 0 / false / null。

final字段没有被赋值,编译器报错 

加上参数(force = true)之后 

生成的字节码文件 

@RequiredArgsConstructor会生成构造方法(可能带参数也可能不带参数),如果带参数,这参数只能是以final修饰的未经初始化的字段,或者是以@NonNull注解的未经初始化的字段 

@RequiredArgsConstructor(staticName = "off")会生成一个off()的静态方法,并把构造方法设置为私有的

不带参数的情况 

带参数的情况 

添加staticName参数的情况

 

 @AllArgsConstructor 生成一个全参数的构造方法

@Data

@Data 包含了 @ToString、@EqualsAndHashCode、@Getter / @Setter和@RequiredArgsConstructor的功能

 @Accessors

@Accessors 主要用于控制生成的getter和setter
主要参数介绍

  • fluent boolean值,默认为false。此字段主要为控制生成的getter和setter方法前面是否带get/set,true表示去掉get/set
  • chain boolean值,默认false。如果设置为true,setter返回的是此对象,方便链式调用方法
  • prefix 设置前缀 例如:@Accessors(prefix = "abc") private String abcAge 当生成get/set方法时,会把此前缀去掉

@Synchronized 

给方法加上同步锁

 

@Builder

使用此注解进行对象的构建,函数式编程/链式编程,省去逐行字段set。

@Builder
public class User{
    private Long id;
    private String name;
    private Integer age;

    public void test1(){
        User.builder().id(1L).name("卤蛋").age(3);
    }

}

@Log

支持Log4j、Slf4j,日志功能

@Log
public class User{
    private Long id;
    private String name;
    private Integer age;

    public static void test(){
        log.info("hello,lombok");
    }

    public static void main(String[] args) {
        test();
    }
}

@Wither

提供了给final字段赋值的一种方法

public class User{
   @With @NonNull private final Long id;
   @With private final String name;

    public User(@NonNull Long id, String name) {
       this.id = id;
       this.name = name;
    }
}

等效代码

public class User {
    @NonNull
    private final Long id;
    private final String name;

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

    public User withId(@NonNull final Long id) {
        if (id == null) {
            throw new NullPointerException("id is marked non-null but is null");
        } else {
            return this.id == id ? this : new User(id, this.name);
        }
    }

    public User withName(final String name) {
        return this.name == name ? this : new User(this.id, name);
    }
}

 就介绍这么多,详细的请看 官方文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鲁蛋儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值