This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

This is the first day of my participation in Gwen Challenge

preface

We write a lot of get/set methods for variables in Java projects. Is there a way to omit them? That’s what Lombok is here to help us do. Let’s take a quick look at Lombok and Lombok’s capabilities.

What is Lombok?

Lombok is a Java library. It also provides IDE plug-in form to simplify code, requiring only one annotation, and eliminating the need to implement getters, setters, or equals methods.

How do I use Lombok?

Using Lombok is very simple. There are only three steps: 1, install the Lombok plug-in, 2, add dependencies, and 3, add annotations to your classes

For clarity, let’s use Idea and Eclipse, two IDE editors, to illustrate them separately.

How do I install Lombok

Idea install Lombok, so there is no need to install Lombok, here we will talk about the previous version

Versions after 2020.3 already have Lombok built in, so you don’t need to install it. Here we’ll look at installing Lombok plug-ins for older versions before 2020.3

  1. Go to File > Settings > Plugins for Browse Repositories
  2. Search Lombok Plugin
  3. Install Lombok Plugin and restart IDEA to complete the installation

The eclipse/myeclipse/STS Lombok installation

To open it, download Lombok.jar, then put lombok.jar in the eclipsemyEclipse/STS root directory and run it

Click Install and wait for the installation to complete. Isn’t that easy?

Add Lombok dependencies

Maven way

  1. Jdk1.8 and below use this dependency
<dependencies>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.18.20</version>
		<scope>provided</scope>
	</dependency>
</dependencies>
Copy the code
  1. Jdk9 or later
<annotationProcessorPaths>
	<path>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.18.20</version>
	</path>
</annotationProcessorPaths>
Copy the code

Gradle way

repositories {
	mavenCentral()
}

dependencies {
	compileOnly 'org. Projectlombok: lombok: 1.18.20'
	annotationProcessor 'org. Projectlombok: lombok: 1.18.20'
	
	testCompileOnly 'org. Projectlombok: lombok: 1.18.20'
	testAnnotationProcessor 'org. Projectlombok: lombok: 1.18.20'
}
Copy the code

Ant way

<javac srcdir="src" destdir="build" source="1.8">
	<classpath location="lib/lombok.jar" />
</javac>
Copy the code

Configure Lombok in Ivy

<dependencies>
	<dependency org="org.projectlombok" name="lombok" rev="1.18.20" conf="build->master" />
</dependencies>
Copy the code

Use Lombok in your code

Annotations and features supported by Lombok

List a few common notes

  1. @NonNull

Never NullPointerException

  1. @Cleanup

Automatic resource management, equivalent to close()

  1. @Getter

Implement the get method of the field

  1. @Setter

Implement the set method of a field

  1. @ToString

Implement the toString method

  1. @EqualsAndHashCode

Implement hashCode and equals

  1. @Data

Equivalent to several annotations together: @toString, @equalSandHashCode, @getter on all fields and @setter, @requiredargsConstructor

Use examples in code

@Getter
@Setter
public class Demo implements Serializable {
    private String id;
}
Copy the code

So we can call getId(), setId(String ID) without having to write set and get.

This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details