Lombok
Lombok reduces java boilerplate code by its annotation. Based on the annotation Lombok generates boiler plate codes for us during compile time. It increases developer productivity, helps developer to invest more time on their business logic.
Release date: January 2021 | Spring Boot: 2.4.x | Revision: 1
Action: Enable Lombok
Lombok build script dependencies and IDE (e.g. Intellij IDEA) lombok plugin both required side by side to use lombok efficiently at development. |
Enable Lombok at IntelliJ IDEA
-
Install Intellij IDEA lombok plugin
-
Enable annotation processor from
.
Example: Use Lombok Annotations
Use lombok instead of generating boilerplate codes. A simple POJO with generated getter and setter will look like:
public class User {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
After using lombok @Getter and @Setter annotation, POJO looks more clean and maintainable. Under the hood lombok generates required getter and setter method based on the lombok annotations during compile time.
@Getter(1)
@Setter(2)
public class User {
private long id;
private String name;
}
1 | Lombok’s annotation @Getter to generate getter for the all class fields. |
2 | Lombok’s annotation @Setter to generate setter for the all class fields. |
Use Delombok to see what code lombok generates for us.
In IntelliJ IDEA right click on the java file which contains lombok annotation, then click .
Lombok will replace those annotations and generate the all boiler plate codes for you.
|
Some useful lombok annotations:
Project Code
-
Github - Lombok