Project Lombok is a java library that automatically plugs into your editor and builds tools, spicing up your java.
The Lombok tool is used for removing the boilerplate logic present in our classes. Which helps to ease the maintainability of our application.
Lombok is a tool and it used to reduce boilerplate code for model/data objects, e.g., it can generate getters and setters for those object automatically. For this Lombok has provided some annotations. All the required method which we need in our any model/Entity class that we can get using Lombok annotation so we no need to write it manually. Lombok also provides the features such as lazy loading, thread safety or immutability.
Annotations provided by Lombok:
You can use val as the type of a local variable declaration instead of actually writing the type.
var works exactly like val, except the local variable is not marked as final.
You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope.
- @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
Dependencies for lombok in Java projects:
In order to add lombok to the classpath of a Java Project with Apache Maven the following dependency has to be added:
In order to add lombok to the classpath of a Java Project with Gradle the following dependency has to be added:
Once we will add this dependency then we are enable Lombok project, and all annotation we can write in your application. But itís not enough to enable Lombok project in our eclipse. So for that we need to configure Lombok in our eclipse IDE.
Follow below steps:
- Go to m2 repository , where Lombok jar stored
- Then go to command prompt and specify path to this directory and then run this Lombok jar with command
- Run command
- After run above command one popup will open like below
- Then click on specify location and then give your eclipse.exe installed path like below
Write your Entity/Model class as below:
So if you mark above dto we manually no need to write setter, getter, toString and constructor manually
These features we can achieve using annotation provided by Lombok tool.