Introduction to the use of lombok

Preface

We often see the application of lombok in projects. This article is a popular science post and introduces lombok in detail.

1. Basic knowledge

简介:
Project Lombok is a tool for Java development designed to simplify writing Java code by automatically generating boilerplate code.
It helps developers automatically generate some common Java codes, such as Getter, Setter, constructor, etc., through annotation, thereby reducing the writing of boilerplate code and improving development efficiency.
工作原理:
Lombok implements the function of automatically generating code during the compilation phase by using annotations in Java source code.
When developers use the annotations provided by Lombok on a class or field, Lombok will insert the AST (Abstract Syntax Tree) operation when compiling the source code and add the corresponding code to the generated Java bytecode. In this way, at run time, the code generated through the annotations is included in the code, and developers do not need to manually write these lengthy codes.

Its advantages and disadvantages are as follows:

advantage shortcoming
1. Simplify the code: Lombok reduces the amount of code developers need to write and improves the simplicity of the code by automatically generating common boilerplate code.
2. Improve development efficiency: By automatically generating code, developers can focus more on business logic, reducing repetitive labor and improving development efficiency.
3. Readability: Since the generated code is standard and consistent, the readability of the code is improved.
1. Hidden details: The automatically generated code may hide some details, and it may not be easy for developers to understand the specific implementation.
2. Not suitable for all scenarios: Lombok is suitable for generating some common codes, but it is not suitable for all scenarios. In some complex cases, manual coding may be required.
3. Tool dependencies: The development team needs to install the Lombok plug-in in the development environment or support Lombok through building tool configurations, which increases the project's tool dependencies.

2. Code application

This plug-in is automatically bundled in the latest version of idea. If lombok is not available and cannot be used, just add it in the software.

First introduce the dependencies in the Maven file:

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>>${lombok.version}</version>
	<scope>provided</scope>
</dependency>

Annotations for lombok mainly include the following:

  • @Setter and @Getter
import lombok.Getter;
import lombok.Setter;

public class MyClass {
    
    
    @Getter
    @Setter
    private String name;
}

Equivalent to:

public class MyClass {
    
    
    private String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}
  • @AllArgsConstructor
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
public class MyClass {
    
    
    private String name;
    private int age;
}

Equivalent to:

public class MyClass {
    
    
    private String name;
    private int age;

    public MyClass() {
    
    
        // 无参构造方法
    }

    public MyClass(String name, int age) {
    
    
        // 有参构造方法
        this.name = name;
        this.age = age;
    }
}
  • @NoArgsConstructor: This annotation is used on classes to provide no-argument construction

  • @Data: (Contains the functions of @Getter, @Setter, @ToString, @EqualsAndHashCode and @AllArgsConstructor)
    It should be noted that after using @Data and @AllArgsConstructor at the same time , the default no-argument constructor is invalid, if you need it, reset @NoArgsConstructor

import lombok.Data;

@Data
public class MyClass {
    
    
    private String name;
    private int age;
}
  • @Log (this is a generic annotation, which has many forms): @Slf4j: After annotating the class, log can be called directly
log.info(xxxx);
  • @ToString: This annotation is used on the class. After compilation, the toString method returns will be output in the form of field name-value.

  • @EqualsAndHashCode: This annotation is used on the class and generates equals and hashCode at the same time.

  • @NonNull: Add non-null judgment

public NonNullExample(@NonNull Person person) {
    
    
	this.name = person.getName();
}

Converts to:

public NonNullExample(@NonNull Person person) {
    
    
	if (person == null) {
    
    
		throw new NullPointerException("person");
	}
	this.name = person.getName();
}
  • @Cleanup: Automatically close the stream, equivalent to try with resource
@Cleanup
InputStream in = new FileInputStream(args[0]);
@Cleanup
OutputStream out = new FileOutputStream(args[1]);
  • @SneakyThrows: When we need to throw an exception, call it on the current method. There is no need to explicitly write throw after the method name.
@SneakyThrows(Exception.class)
  • @Synchronized: All the code in the method is added to a code block. The default static method uses a global lock, and the ordinary method uses an object lock. Of course, The object of the lock can be specified.
private final Object lock = new Object();
	@Synchronized("lock")
	public void foo() {
    
    
		// Do something
	}

Guess you like

Origin blog.csdn.net/weixin_47872288/article/details/134532104