The most complete lombok annotation details (updated from time to time with the version)
1. Annotation introduction
@Builder annotation to generate relatively slightly complex builder APIs
- It acts on the class, turning it into the builder pattern
- Can be called in chain form
- The object generated by initializing the instance object cannot be changed, and can be assigned when creating the object.
- If you need to modify it on the original basis, you can add the set method, and the final field does not require initialization.
- It generates a constructor with full parameters
2. Introduction to attributes
-
@: Non-final fields can have default values
-
builderMethodName: Specify the method name for creating an internal static class, the default value is builder
-
buildMethodName: Specify the method name for creating entity class, the default value is build
-
builderClassName: Specify the internal static class name, the default value is "", and the default created class name is thisclassBuilder
-
toBuilder: Set to true to copy this object to generate a new object, which can be modified again, and the default is false
-
access: Set the access permission modifier of builderMethodName, default to public
There are PUBLIC, MODULE, PROTECTED, PACKAGE, and PRIVATE, among which MODULE is a new feature of Java 9. -
setterPrefix: Set the prefix of the setter method, default to ""
3. Practical drills
@Builder(
builderMethodName = "builder", buildMethodName = "build", builderClassName = "",
toBuilder = true, access = AccessLevel.PUBLIC, setterPrefix = ""
)
public class Yifei {
@
private String name = "Liu Yifei";
private String sex;
private final Integer age = 18; // Default can be initialized successfully if the final field is added or not.
public static void main(String[] args) {
Yifei yifei = Yifei.builder().build(); // If no Default is added, the output name result is null.
/**
* Similar to copy, the name value has been modified, the age value is still the same
* If toBuilder = false, there is no toBuilder method
*/
yifei = yifei.toBuilder().name("Liu Yifei").build();
}
}
We often use generics in our daily development, and @Builder also supports specified generic construction
@Builder
public class Yifei<T> {
public void yiyang(Yifei<T> yi) {
yi = Yifei.<T>builder().builder();
}
}
@Builder will generate a full parameter construction method, so there is no parameterless construction method, but when we encounter a method without parameterless construction method, problems will occur. At this time, handwriting or adding@NoArgsConstructorThere will be errors, two solutions
1. Add@AllArgsConstructor
@Builder
@AllArgsConstructor
public class Yifei {
public Yifei() {
/**
* Handwriting or @NoArgsConstructor will invalidate the full parameter constructor generated by @Builder
* At this time, an error was reported, and you can add @AllArgsConstructor or write a full parameter constructor by hand.
*/
}
}
2. Use@Tolerateannotation
4. Attribute expansion
lombok basic annotation @
5. What is done inside @Builder
- Create an internal static class called ThisClassBuilder with the same properties as the entity class (called the builder)
- In the builder: for all properties in the target class and uninitialized final fields, corresponding properties are created in the builder.
- In the builder: Create a default constructor without parameters
- In the builder: Each parameter in the entity class will create a setter-like method, with the same method name as the parameter name. And the return value is the builder itself (for chained calls)
- In the builder: a build method will be created, the build method will be called, and the entity object will be created based on the set value
- In the builder: a toString method is generated
- In entity class: a builder method is created, and its purpose is to create a builder
@Builder
public class User {
private String username;
private String password;
}
After compilation
public class User {
private String username;
private String password;
User(String username, String password) {
this.username = username;
this.password = password;
}
public static User.UserBuilder builder() {
return new User.UserBuilder();
}
public static class UserBuilder {
private String username;
private String password;
UserBuilder() {}
public User.UserBuilder username(String username) {
this.username = username;
return this;
}
public User.UserBuilder password(String password) {
this.password = password;
return this;
}
public User build() {
return new User(this.username, this.password);
}
public String toString() {
return "(username=" + this.username + ", password=" + this.password + ")";
}
}
}