Lombok @RequiredArgsConstructor examples

  1. Lombok @RequiredArgsConstructor
    Generates a constructor with required arguments. Required arguments are uninitialized final fields and fields with constraints such as @NonNull. Default access modifier is public.

Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.

Lombok 注解:

@RequiredArgsConstructor
public class RequiredArgsDemo1 {

  private Long id;

  @NonNull
  private String username;

  @NonNull
  private String email;

  private final boolean status;
}

等价于:

public class RequiredArgsDemo1 {
  private Long id;
  @NonNull
  private String username;
  @NonNull
  private String email;
  private final boolean status;

  public RequiredArgsDemo1(
      @NonNull final String username, 
      @NonNull final String email, 
      final boolean status
      ) {
    
    if (username == null) {
      throw new NullPointerException("username is marked non-null but is null");
    }
    if (email == null) {
      throw new NullPointerException("email is marked non-null but is null");
    }
    this.username = username;
    this.email = email;
    this.status = status;
  }
}
  1. Lombok @RequiredArgsConstructor vs non-final and static fields
    Lombok @RequiredArgsConstructor will not generate any argument for following fields

(1)Non-final fields.
(2)Initialized final fields.
(3)static fields.
(4)Initialized non-null fields.

Lombok 注解:

@RequiredArgsConstructor
public class RequiredArgsDemo2 {

  private Long id;

  @NonNull
  private String username = "anonymous";

  private final int defaultRole = 1;
  
  private static double minSalary = 10000.00;
}

等价于:

public class RequiredArgsDemo2 {
  private Long id;
  @NonNull
  private String username = "anonymous";
  private final int defaultRole = 1;
  private static double minSalary = 10000.0;

  public RequiredArgsDemo2() {
  }
}
  1. Generating private constructor using @RequiredArgsConstructor
    Lombok generates a public constructor by default for the @RequiredArgsConstructor. To generate private constructor declare @RequiredArgsConstructor(access = AccessLevel.PRIVATE). access attribute of @RequiredArgsConstructor allows you to change the access modifier of the generated constructor.

Lomboked RequiredArgsDemo3.java:

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class RequiredArgsDemo3 {

  private Long id;

  @NonNull
  private String username;
  
}

Delomboked RequiredArgsDemo3.java

public class RequiredArgsDemo3 {
  private Long id;
  @NonNull
  private String username;

  private RequiredArgsDemo3(@NonNull final String username) {
    if (username == null) {
      throw new NullPointerException("username is marked non-null but is null");
    }
    this.username = username;
  }
}
  1. Creating a static factory method using @RequiredArgsConstructor

If we would like to create instance using a static factory method, staticName attribute of @RequiredArgsConstructor allows us to generates a private constructor with one argument for each uninitialized final, non-null fields and an additional static factory method that wraps around the private constructor is generated. Let’s have a look into following example.

Lomboked RequiredArgsDemo4.java

@RequiredArgsConstructor(staticName = "getInstance")
public class RequiredArgsDemo4 {

  private Long id;

  @NonNull
  private String username;

  @NonNull
  private String email;

  private final boolean status;
  
}

Delomboked RequiredArgsDemo4.java

public class RequiredArgsDemo4 {
  private Long id;
  @NonNull
  private String username;
  @NonNull
  private String email;
  private final boolean status;

  private RequiredArgsDemo4(
      @NonNull final String username,
      @NonNull final String email,
      final boolean status) {
    
    if (username == null) {
      throw new NullPointerException("username is marked non-null but is null");
    }
    if (email == null) {
      throw new NullPointerException("email is marked non-null but is null");
    }
    this.username = username;
    this.email = email;
    this.status = status;
  }

  public static RequiredArgsDemo4 getInstance(
      @NonNull final String username, 
      @NonNull final String email, 
      final boolean status) {
    
    return new RequiredArgsDemo4(username, email, status);
  }
}
  1. Put annotations on constructor generated by @RequiredArgsConstructor

Sometimes you may want to define annotations on top of constructor, for example when you are working with Spring framework or some other third party java libraries, you may need to declare annotations on top of constructor. onConstructor attribute of @RequiredArgsConstructor allows us to put annotations on generated required-args constructor.

Up to JDK7: @NoArgsConstructor(onConstructor=@__({@AnnotationsGoHere}))
From JDK8: @NoArgsConstructor(onConstructor_={@AnnotationsGohere}) // note the underscore after onConstructor.

Lomboked RequiredArgsDemo5.java

@RequiredArgsConstructor(
    onConstructor_=
    @ConstructorProperties({"username"}))
public class RequiredArgsDemo5 {

  private Long id;

  @NonNull
  private String username;

}

Delomboked RequiredArgsDemo5.java

public class RequiredArgsDemo5 {
  private Long id;
  @NonNull
  private String username;

  @ConstructorProperties({"username"})
  public RequiredArgsDemo5(@NonNull final String username) {
    if (username == null) {
      throw new NullPointerException("username is marked non-null but is null");
    }
    this.username = username;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值