The article directories

  • Replace three types of boilerplate code with annotations
  • Lombok installation
    • 2.1 Importing maven packages
    • 2.2 Adding IDE tool support for Lombok
    • 2.3 Implementation Principles of Lombok
  • Use of Lombok annotations
    • 3.1 Common annotations for POJO classes
      • 3.1.1 @getter / @setter + @toString (most commonly used)
      • @equalSandHashCode + @nonnull + @noargsconstructor
      • 3.1.3 @ Builder + @ the Log
    • Close the stream in the 3.2@cleanup Finally block
    • 3.3 @sneakythrows exception capture
    • 3.4 @synchronized The synchronization method
  • Fourth, the end

Replace three types of boilerplate code with annotations

Previous Java projects were full of unfriendly code: getters/setters/toStrings for POjos; Exception handling; These are the three most common boilerplate types of code that are neither technical nor aesthetically pleasing, and Lombok offers a way to use annotations instead.

Lombok installation

2.1 Importing maven packages

< the dependency > < groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.16.18 < / version > <scope>provided</scope> </dependency>Copy the code

Note Lombok’s scope=provided, which means it only works at compile time and does not need to be typed into the package. In fact, Lombok correctly compiles lombok-annotated Java files into full Class files at compile time.

2.2 Adding IDE tool support for Lombok

Lombok support is introduced in IDEA as follows:

Click on the File– Settings Settings screen to install Lombok:

Click on the File– Settings Settings screen to enable AnnocationProcessors:

This is turned on to allow Lombok annotations to come into play at compile time.

2.3 Implementation Principles of Lombok

Since Java 6, Javac has supported the “JSR 269 Pluggable Annotation Processing API” specification, which can be called whenever a program implements the API while Javac is running.

Lombok is a program that implements the “JSR 269 API”. When using JavAC, it works as follows:

  1. Javac analyzes the source code to generate an abstract syntax tree (AST)
  2. The Lombok program that implements JSR 269 is invoked during javAC compilation
  3. Lombok then processes the AST obtained in the first step, finds the syntax tree (AST) corresponding to Lombok’s annotation class, and then modifies the AST to add the corresponding tree nodes defined by Lombok annotations
  4. Javac uses a modified abstract syntax tree (AST) to generate bytecode files

Use of Lombok annotations

3.1 Common annotations for POJO classes

3.1.1 @getter / @setter + @toString (most commonly used)

@getter / @setter: generates Getter/ Setter methods for all member variables on a class. Function on a member variable to generate getter/setter methods for that member variable. You can set the access permission and whether lazy loading.

package com.kaplan.pojo;

import lombok.*;
import lombok.extern.log4j.Log4j;
@Getter
@Setter
public class TestDemo {
private String name;
private int age ; 
private String email;
private String address; 
private String password;

@Getter
@Setter
private boolean funny;
}
Copy the code

ToString: Overrides the default ToString () method of a class, qualifying certain fields with the of attribute and excluding certain fields with the exclude attribute.

@equalSandHashCode + @nonnull + @noargsconstructor

@equalsandHashCode: Applied to the class, overriding the default equals and hashCode

@nonNULL: applies to member variables and parameters. The identifier cannot be null. Otherwise, a null pointer exception is thrown.

NoArgsConstructor, @requiredargsconstructor, @allargsconstructor: Applied to the class to generate the constructor. The attributes include staticName and access. Once the staticName attribute is set, the instance will be generated using a static method, and the Access attribute can restrict access.

@noargsconstructor: Generates a no-parameter constructor;

RequiredArgsConstructor: generates a constructor for member variables containing final and @nonnull annotations;

AllArgsConstructor: Generates a full-parameter constructor

Note that @data: applies to a class and is a collection of annotations: @getter @setter @toString @equalSandhashCode @requiredargsconstructor

3.1.3 @ Builder + @ the Log

Builder: Works on a class to convert it to Builder mode

@log: applied to a class to generate a Log variable. There are different annotations for different logging implementation products:

Close the stream in the 3.2@cleanup Finally block

Cleanup: Automatically closes resources for objects that implement the java.io.Closeable interface, such as typical IO stream objects

The result is as follows:

3.3 @sneakythrows exception capture

@sneakythrows: Can catch checked exceptions and throw, can rewrite the above main method as follows:

3.4 @synchronized The synchronization method

Synchronized: used at the method level to replace the synchronize keyword or lock, which is not very useful.

Fourth, the end

Efficient development: Use of Lombok, done.

Play code every day, progress every day.