Lombok super detailed explanation

Table of contents

1. Overview of Lombok

2. Lombok plug-in installation

3. Comments on Lombok

3.1 @Setter and @Getter 

3.2 @ToString

3.3 @EqualsAndHashCode,@NonNull

3.4 @NoArgsConstructor,@RequiredArgsConstructor,@AllArgsConstructor

3.5 @Data

3.6 @Builder

3.7 @Log

3.8 @CleanUp

3.9 @SneakyThrows


1. Overview of Lombok

        Previous Java projects were filled with too much unfriendly code: POJO's getter/setter/toString/constructor methods; printing logs; closing operations of I/O streams, etc. These codes have no technical content and affect the code. For the beauty, Lombok came into being. LomBok can help developers eliminate lengthy code in JAVA, especially in POJO classes, through annotations.

Before using LomBok

After using LomBok

2. Lombok plug-in installation

If the IDEA version is 2020.3 or above, there is no need to install the Lombok plug-in. If the IDEA version is below 2020.3, you need to install the Lombok plug-in. The installation method is as follows:

  1. Click Flie->Setting->Plugins
  2. Search for Lombok and install it

3. Comments on Lombok

The Lombok dependencies of ordinary maven projects are:

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

The SpringBoot project Lombok is introduced as follows:

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency> 

3.1 @Setter and @Getter 

Function: Provide setter/getter methods for attributes in the class

Position: above the class or above the attribute. Above the attribute, setter/getter methods are generated for the attribute.
. Above the class, it means that setter/getter methods are generated for all attributes under the class.
Attribute: Set setter and getter access permissions

//给类下的所有属性添加Setter/Getter
@Setter
@Getter
public class User {
  //给id属性添加Setter
  @Setter
  private Integer id;
  //给username的setter方法设置私有权限
  @Setter(AccessLevel.PRIVATE)
  private String username;
  //取消password的Getter方法
  @Getter(AccessLevel.NONE)
  private String password;
  private static int age;
  private final String address = null;
}

Take a look at the structure of this class:

Note:

  • Static modified variables do not generate getter and setter methods
  • Final modified variables only generate getter methods 

3.2 @ToString

function: generate toString method, by default it will print the class name and each field in order.
Position: above the class
Attribute: exclude: Cancel the display of one or more variables in the toString method

After testing, it does not appear 

3.3 @EqualsAndHashCode,@NonNull

Determine whether two objects are equal
In Java, calling equals() can determine whether two objects are equal. If the class does not override this method, it determines whether the two references point to the same object.
How to rewrite equals():

  • Determine whether two references point to the same object
  • Determine whether the reference is Null
  • To determine whether the actual types of two objects are equal, you need to call canEqual()
  • Determine whether the properties of two objects are equal

To determine whether an object is repeated in a Set, you need to call hashCode() to calculate the hash value before calling equals(). Therefore, judging the equality of objects requires rewriting the three methods equals(), canEqual(), and hashCode().
@EqualsAndHashCode
Function: Generate equals, hashCode, and canEqual methods. Used to compare whether two class objects are the same.
Position: above the class
Attribute: exclude: exclude some attributes when comparing, of: use only some attributes when comparing

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
//@EqualsAndHashCode(exclude = {"password"})
//排除password,只使用id,username对比及计算hash
@EqualsAndHashCode(of = {"username"})
//只使用username对比及计算hash
public class User3 {
    private Integer id;
    private String username;
    private String password;
}

@NonNull

Function: used before method parameters, indicating that the parameters cannot be null when calling the method; used above the attribute, indicating that the value cannot be null when assigning a value to the attribute.
Position: before method parameters or above attributes. 

3.4 @NoArgsConstructor,@RequiredArgsConstructor,@AllArgsConstructor

@NoArgsConstructor

Function: Generate parameterless constructor
Position: Above the class
@RequiredArgsConstructor
Function: Generate final and @NonNull modified attributes
Position: above the class
@AllArgsConstructor
Function: Generate a fully parameterized constructor
Position: above class

3.5 @Data

Function: Equivalent to adding five annotations @Setter, @Getter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor at the same time
Position: Above the class

3.6 @Builder

Function: Provide chain-style object creation
Location: above the class

Sample code:

// 同时提供@Setter、@Getter、@ToString、
@EqualsAndHashCode、@RequiredArgsConstructor
@Data
// 提供链式风格创建对象
@Builder
public class User6 {
  @NonNull
  private Integer id;
  private String username;
  private String password;
}
// 测试
@Test
public void testUser() {
  User6 user6 = User6.builder()
   .id(1)
   .username("itbaizhan")
   .password("itbaizhan")
   .build();
}

3.7 @Log

Function: Generate a log object in the class, which can be used directly in the method
Location: above the class

Note: There are different log annotations for different log implementation products. Using @Log means using the log function that comes with Java. In addition to @Log, you can also use @Log4j, @Log4j2, @Slf4j and other annotations to use different logs. product.

3.8 @CleanUp

Function: Automatically close resources, such as IO stream objects.
Position: in front of the code

3.9 @SneakyThrows

Function: Catching and throwing exceptions in methods
Location: Above the method

Guess you like

Origin blog.csdn.net/qq_53317005/article/details/134431160