Detailed Java Lombok

1. Introduction to Lombok

The Lombok project is a Java library that is automatically inserted into editors and build tools. Lombok provides a set of useful comments to eliminate a lot of boilerplate code in Java classes. For example, using @Data can replace hundreds of lines of code to produce clean, concise and easy to maintain Java classes.

2. Use of Lombok

  • Introduce the corresponding maven package
<denpendency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
    <scope>provided</scope>
</dependency>

Lombok's scope=provided means that it only takes effect during the compilation phase and does not need to be included in the package. This is actually the case. Lombok correctly compiles Java files with Lombok annotations into complete Class files during compilation.

  • Add IDEA tool support for Lombok

 Click File--->Settings--->Plugins--->Browse repositories, install Lombok plug-in;

Click File--->Settings to open AnnocationProcessors;

This option is enabled to allow Lombok annotations to play a role in the compilation phase.

 3. Lombok annotation

  • @Getter/@Setter: On the function class, generate the getter/setter methods of all member variables; acting on the member variable, generate the getter/setter methods of the member variable. You can set access permissions and whether to lazy loading, etc.
  • @ToString: Acting on the class, overriding the default toString() method, you can limit the display of certain fields through the of attribute, and exclude certain fields through the exclude attribute.
  • @EqualsAndHashCode: Acting on the class, overriding the default equals and hashCode.
  • @NonNull: Mainly used in member variables and parameters. The identifier cannot be null, otherwise a null pointer exception will be thrown.
  • @NoArgsConstructor: Annotate in the class to generate a construction method without parameters.
  • @RequiredArgsConstructor: Annotate in the class, generate construction methods for fields in the class that require special treatment, such as final and @NonNull annotated fields.
  • @AllArgsConstructor: Annotate in the class to generate a constructor that contains all the fields in the class.
  • @Data: Acting on the class, it is a collection of @ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor, which generates setter/getter, equals, canEqual, hashCode, and toString methods. If it is a final property, no setter method will be generated for the property .
  • @Log: Act on the class to generate log variables. There are different annotations for different log implementation products.

 

 

Guess you like

Origin blog.csdn.net/li_w_ch/article/details/108681329