springboot依赖lombok插件、lombok常用注解

Lombok是一个能够自动插入编辑器和构建工具的Java库,旨在简化Java代码,减少getter、setter等样板代码的编写。通过@NotNull、@NoArgsConstructor、@RequiredArgsConstructor、@AllArgsConstructor、@Getter、@Setter等注解,可以快速生成构造方法、getter和setter等。此外,Lombok还提供了@ToString、@SneakyThrows、@Synchronized等注解,用于生成toString方法、处理异常和同步方法。@Data和@Value注解则能自动生成类的完整逻辑,包括equals和hashCode。Lombok的使用极大地提高了开发效率和代码整洁度。

 ——springboot依赖lombok插件、lombok常用注解

1 lombok插件

1.1 lombok插件简介

        官方介绍如下:

        意思是:lombok是一个能自动插入到编辑器和构建工具的java库。目的是为了简化java代码,开发者不需要写setter、getter和构造方法等,只需要几个或多个lombok注解就能实现。

         Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。例如开发中经常需要写的pojo,都需要花时间去添加相应的getter、setter、构造器和equals等方法,当属性多时会出现大量的getter/setter方法,维护起来很麻烦,也容易出错。

1.2 springboot依赖lombok插件

        springboot中只需要依赖lombok的jar包就可使用Lombok。

        (1)方法一:可以在官网(Download)下载jar包:

        (2)方法二: 可以使用maven添加依赖:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.22</version>
    <scope>provided</scope>
</dependency>

2 lombok常用注解

2.1 @NotNull

        @NotNull注解如果设置在一个属性上,lombok将在自动生成的方法/构造函数体的开头插入一个空检查,抛出NullPointerException,并将参数的名称作为message。

lombok注解实现:

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;

@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
  private int x, y;
  @NonNull private T description;
  
  @NoArgsConstructor
  public static class NoArgsExample {
    @NonNull private String field;
  }
}

等价代码:

public class ConstructorExample<T> {
  private int x, y;
  @NonNull private T description;
  
  private ConstructorExample(T description) {
    //空检查
    if (description == null) throw new NullPointerException("description");
    this.description = description;
  }
  
  public static <T> ConstructorExample<T> of(T description) {
    return new ConstructorExample<T>(description);
  }
  
  @java.beans.ConstructorProperties({"x", "y", "description"})
  protected ConstructorExample(int x, int y, T description) {
    //空检查
    if (description == null) throw new NullPointerException("description");
    this.x = x;
    this.y = y;
    this.description = description;
  }
  
  public static class NoArgsExample {
    @NonNull private String field;
    
    public NoArgsExample() {
    }
  }
}

2.2 @NoArgsConstructor、@RequiredArgsConstructor和@AllArgsConstructor

2.2.1 @NoArgsConstructor

        生成不带参数的构造方法,即:无参构造方法。@NoArgsConstructor等价于:

public MessageInfo() {
}

        如果存在被final修饰的属性,那么会编译失败。

         可以设置force属性值为true,来避免编译错误。但是所有final修饰的属性会被初始化为final字段初始化为:0 或null 或 false。也就是说包装类型会被初始化为null,简单类型初始化为0或false。

2.2.2 @RequiredArgsConstructor

        只为final / @non-null修饰字段(不包括static修饰的)生成带参数的构造方法。

2.2.3 @AllArgsConstructor

描述:为所有字段(包括final修饰的,不包括static修饰的)生成带参数的构造方法,即:全参数构造方法。

2.3 @Getter和@Setter

         lombok官方文档的描述:

 lombok注解实现:

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {
  /**
   * Age of the person. Water is wet.
   * 
   * @param age New value for this person's age. Sky is blue.
   * @return The current value of this person's age. Circles are round.
   */
  @Getter @Setter private int age = 10;
  
  /**
   * Name of the person.
   * -- SETTER --
   * Changes the name of this person.
   * 
   * @param name The new value.
   */
  @Setter(AccessLevel.PROTECTED) private String name;
  
  @Override public String toString() {
    return String.format("%s (age: %d)", name, age);
  }
}

等价代码:

public class GetterSetterExample {
  /**
   * A
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT_Most

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

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

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

打赏作者

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

抵扣说明:

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

余额充值