Using @Data annotation in Project Lombok

You are tired of declaring the Getter, Setter, toString(), equals() and hashCode() method for each class that we want to use these method. Using @Data annotation in Project Lombok can help us avoid this problem.

Let create a Maven project for example:

Using @Data annotation in Project Lombok

With Project Lombok dependency as below:

Remember that, in order to the IDE can understand the code, we need install Project Lombok plugin into our IDE. See construction for IntelliJ IDEA at here.

Now, for example, I have a Student class with some information like firstName, middleName, lastName and country. Without using Project Lombok library, I must declare Student bean as below:

With Project Lombok, we can remove all of these methods here. Just with @Data annotation in Project Lombok.

Very simple and clean.

If you check the Student.class file in /target/classes/com/huongdanjava/lombok, you will see the content as below:

Chia sẽ bài viết này ...Share on Facebook
0Tweet about this on Twitter
Share on LinkedIn

Add Comment