- 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;
}
}
- 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() {
}
}
- 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;
}
}
- 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);
}
}
- 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;
}
}