【SpringBoot】- SpringBoot中Lombok使用

Lombok是一个Java库,通过注解自动生成getter、setter等方法,简化编码工作。支持@NonNull、@SneakyThrows等注解,提高开发效率。

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

一、认识Lombok

1、概述:

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官网

  • 中文意思是:Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发。通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量;也就是说可以通过注解的方式简化代码;

2、Lombok注解介绍

@Getter/@Setter  注解在属性上,自动生成get、set方法;Getter(所有字段)
Setter (所有非final字段)
@Data   注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法,Data一个注解也就完成了下面五个注解完成的事情
( @ToString
   @EqualsAndHashCode   
   @Getter(所有字段)
   @Setter (所有非final字段)
   @RequiredArgsConstructor
 )
@AIlArgSConstructor    注解在类上;为类提供一个全参的构造方法
@RequiredArgsConstructor and
@NoArgsConstructor     注解在类上;为类提供一个无参的构造方法
@NonNull    是否为空判断,为空则抛出java.lang.NullPointerException
@Log4j   日志注解
@SLf4j   日志注解
@Builder   生成建造者模式(Builder)
@Cleanup  注释可用于确保已分配的资源被释放,生成的代码会把变量用try{}包围,并在finallly块中调用close()
@SneakyThrows 使用try{}catch捕获异常

二、应用

1、添加依赖

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

2、安装Lombok

File–setting–Editor–Plugins–Lombok
在这里插入图片描述
3、使用

  1. @Data
    在这里插入图片描述
    例子-使用注解后
@Data
public class Example {

    private int foo;
    private final String bar;
}

等价源码-使用前

public class Example {
    private int foo;
    private final String bar;

    public Example(String bar) {
        this.bar = bar;
    }

    public int getFoo() {
        return this.foo;
    }

    public String getBar() {
        return this.bar;
    }

    public void setFoo(int foo) {
        this.foo = foo;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Example)) {
            return false;
        } else {
            Example other = (Example)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getFoo() != other.getFoo()) {
                return false;
            } else {
                Object this$bar = this.getBar();
                Object other$bar = other.getBar();
                if (this$bar == null) {
                    if (other$bar != null) {
                        return false;
                    }
                } else if (!this$bar.equals(other$bar)) {
                    return false;
                }

                return true;
            }
        }
    }

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

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

    public String toString() {
        return "Example(foo=" + this.getFoo() + ", bar=" + this.getBar() + ")";
    }
}
  1. @geter,@seter

例子-使用后:

public class Example{
	@Getter @Setter
	private int foo;
}

等价源码-使用前:

public int getFoo() {
        return foo;
    }
 
public void setFoo(int foo) {
        this.foo= foo;
}
  1. @NonNull

实例-使用注解后:

import lombok.NonNull;
public class NonNullExample extends Something {
        private String Name;  
        public NonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
    }
}

等价源码-使用前:

public class NonNullExample extends Something {
    private String name;  
    public NonNullExample(@NonNull Person person) {
        super("Hello");
        if (person == null) {
            throw new NullPointerException("person");
        }
        this.name = person.getName();
    }
}
  1. @SneakyThrows

实例:

@SneakyThrows(UnsupportedEncodingException.class)
public void utf8ToString(byte[] bytes) {
     return new String(bytes, "UTF-8");
}

等价源码:

public void utf8ToString(byte[] bytes) {
     try {
         return new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException $uniqueName) {
         throw useMagicTrickeryToHideThisFromTheCompiler($uniqueName);
}    

进一步了解认识注解的作用帮助我们进行开发,事半功倍;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值