DEV Community

Cover image for Starting with Project Lombok
ChristianBlanco
ChristianBlanco

Posted on • Updated on

Starting with Project Lombok

In this tutorial we are going to start using Lombok as part of our code, first we are going to introduce what Lombok is, then how to add it into a Spring Boot project, and then show some of the annotations that we can use into our code in order to avoid boilerplate code.

What is Lombok?

Lombok is a library that we can manage as a new dependency inside of our project. The main objective of this library is to use annotations in order to avoid boilerplate code. We have a lot of it, right? It was created in 2009 and after all this years its popularity and use has increased a lot. Now days, we can see a lot of new Java projects using it, and also a lot of legacy projects that want to update how are they writing code and try to use this kind of libraries in order to make the code more readable and easy to understand. As we said, Lombok uses annotations, yes, like @Override, @NotNull, or @Autowired in Spring. This annotations helps to the class to keep it super clean and easy to understand, we are going to see some examples later in this tutorial.

How to add Lombok to my project?

We are going tu assume that you already know how to create a Spring or Spring Boot project, for this example we are going to use a Spring Boot project builded with Maven, so we are going to move to the part where you want to add the Lombok library. Of course, as always you will have the normal structure where you are going to find the pom.xml file inside your project. In order to use the Lombok library you have to add the corresponding dependency under the dependencies tag, check the image below. Also you can find a better example of how to add the dependency here.
Lombok dependency

After you add the dependency don't forget to update the maven stuff in order to download the dependency for your project. Now we can start using some annotations. For this tutorial we are going to see some of them, specifically: @Getter, @Setter and @RequiredArgsConstructor.

Using Lombok in your code, some examples.

Now, let's use Lombok into the code, as we said, we are using a Spring Boot project to manage this tutorial.

The @Getter and @Setter annotation.

@Getter and @Setter are the more used annotations from Lombok, its objective is to create all the getter and setter methods of the specified fields inside a class, this annotations are very useful when we are creating POJOs, some examples are the ones that we create for entities or data transfer objects (DTOs). Imagine that we have a class called User, this is an entity class, of wourse we want to have the normal fields for this class, like id, email or password, well that could be something like this:

public class User {

    private Long id;

    private String email;

    private String password;

}
Enter fullscreen mode Exit fullscreen mode

And of course, we need to add the getters and setters for those fields:

public class User {

    private Long id;

    private String email;

    private String password;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return this.id;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return this.email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Long getPassword() {
        return this.password;
    }

}
Enter fullscreen mode Exit fullscreen mode

The boilerplate code starts when we have to write all the getters and setters of all the POJOs that we have into our project, here is when Lombok enters in action. Instead of write these getter and setter methods we use the Lombok annotations @Getter and @Setter:

@Getter
@Setter
public class User {

    private Long id;

    private String email;

    private String password;

}
Enter fullscreen mode Exit fullscreen mode

This keeps our class clean and also ease to read and manage in the future, Lombok will help to the JVM to understand the annotations and will create the necessary code for us when compiling.

The @RequiredArgsConstructor annotation and some others.

Let's talk about @RequiredArgsContructor. So, again, remember that we are using Spring Boot, so you want to use dependency injection, for sure, using @Autowired, but you know that there are three ways of how to use @Autowired. The first one and less recommended is using @Autowired at the top of a field, this is not recomendded because sometimes this can lead to a SOLID violation. So you use the @Autowired at the top of a setter when the fields are optional or at the top of the constructor when the fields are required to be injected into the class in order to create a correct functionality.

Okay, let's imagine that we have some final fields, this means that these fields are required to be injected into a class in order to that class can work fine, so we have:

public class UserService {

    private final UserRepository userRepository;

}
Enter fullscreen mode Exit fullscreen mode

For instance, we need a contructor for this final field in order to be initialized:

public class UserService {

    @Autowired
    public UserService(UserRepository userRepository) {

        this.userRepository = userRepository;

    }
}
Enter fullscreen mode Exit fullscreen mode

And of course we are using the @Autowired annotation to inject the field, but now imagine that you use more than one final field (but be careful, not to much fields there to not violate SOLID). We again have more boilerplate code, here Lombok comes to the rescue. We introduce the use of @RequiredArgsConstructor.

@RequiredArgsContructor
public class UserService {

    private final UserRepository userRepository;

}
Enter fullscreen mode Exit fullscreen mode

This annotation will help us to create a constructor with all the required parameters, in this case the final fields. Also if you want not only a constructor with parameters but a constructor without any parameter you can use the @NoArgsContructor annotation too, you can find more information here.

Conclusion

We have seen how Lombok can help us to avoid boilerplate code, could be a good practice for your next projects, try to check all the links that we added along this tutorial in order to understand more about what are we talking about, please leave a comment and let me know if you have any questions. Also please find more about lombok here. Cheers!

Top comments (0)