00:00

Spring Boot Using Lombok – Detailed Explanation

Project Lombok is a Java library that helps reduce boilerplate code in Spring Boot applications by generating commonly used methods at compile time.


What is Lombok?

Lombok automatically generates:

  • Getters and Setters
  • Constructors
  • toString()
  • equals() and hashCode()
  • Builder pattern
  • Logger variables

Lombok works at compile time, so there is no runtime performance impact.


Why Use Lombok in Spring Boot?

  • Reduces boilerplate code
  • Improves code readability
  • Faster development
  • Cleaner Git commits
  • Lower chance of human errors

Adding Lombok to Spring Boot Project

Maven Dependency


    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    

The optional=true flag ensures Lombok is not packaged in the final JAR.


IDE Configuration

  • IntelliJ IDEA: Install Lombok plugin and enable Annotation Processing
  • Eclipse: Install Lombok and enable annotation processing

Common Lombok Annotations

1. @Getter and @Setter

Generates getter and setter methods automatically.


    @Getter
    @Setter
    public class User {
        private Long id;
        private String name;
    }
    

2. @NoArgsConstructor

Generates a no-argument constructor. Required for JPA and Jackson.


    @NoArgsConstructor
    public class User {
    }
    

3. @AllArgsConstructor

Generates a constructor with all fields.


    @AllArgsConstructor
    public class User {
        private Long id;
        private String name;
    }
    

4. @RequiredArgsConstructor

Generates a constructor for final and @NonNull fields.


    @RequiredArgsConstructor
    @Service
    public class OrderService {
        private final OrderRepository orderRepository;
    }
    

This is the recommended approach for Spring dependency injection.


5. @ToString

Generates the toString() method.


    @ToString
    public class User {
        private Long id;
        private String name;
    }
    

Avoid using it on JPA entities with relationships.


6. @EqualsAndHashCode

Generates equals() and hashCode() methods.


    @EqualsAndHashCode(onlyExplicitlyIncluded = true)
    public class User {
        @EqualsAndHashCode.Include
        private Long id;
    }
    

7. @Data

Shortcut annotation that combines multiple Lombok features.


    @Data
    public class UserDTO {
        private Long id;
        private String name;
    }
    

Recommended for DTOs, but not for JPA entities.


8. @Builder

Implements the Builder pattern for object creation.


    @Builder
    public class UserDTO {
        private Long id;
        private String name;
    }
    

Usage:


    UserDTO user = UserDTO.builder()
            .id(1L)
            .name("Sandeep")
            .build();
    

9. @Slf4j

Automatically creates a logger instance.


    @Slf4j
    @Service
    public class UserService {
    
        public void saveUser() {
            log.info("Saving user");
        }
    }
    

10. @NonNull

Adds null-check validation automatically.


    public User(@NonNull String name) {
    }
    

Lombok Usage by Application Layer

Entity Layer (JPA)


    @Entity
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
        @Id
        private Long id;
        private String name;
    }
    

Avoid using @Data and @ToString on entities.


DTO Layer


    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public class UserDTO {
        private Long id;
        private String name;
    }
    

Service Layer


    @Service
    @RequiredArgsConstructor
    @Slf4j
    public class UserService {
        private final UserRepository userRepository;
    }
    

Lombok Interview Questions (Quick)

  • Does Lombok affect performance? No, it works at compile time.
  • Is Lombok production safe? Yes.
  • Why avoid @Data in entities? Can cause lazy loading and recursion issues.

Pros and Cons of Lombok

Pros

  • Less boilerplate
  • Cleaner code
  • Faster development

Cons

  • IDE dependency
  • Overuse can hide logic

Summary

Lombok simplifies Spring Boot development by reducing repetitive code. Use it wisely by selecting appropriate annotations based on application layers.