2.4 Boosting Productivity with Lombok
Lombok is an extremely useful library that automatically generates repetitive code (Boilerplate code) through annotations. It is considered almost essential in Spring Boot projects.
1. Evolution of Data Classes (Before vs After)
Without Lombok, you have to manually write Getters, Setters, Constructors, and toString methods every time you add a field.
Without Lombok (Plain Java)
public class User {
private Long id;
private String name;
public User(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
// ... dozens of lines needed for toString, equals, hashCode, etc.
}
With Lombok
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
2. Detailed Explanation of Core & Advanced Annotations
1) @Getter / @Setter
- Generates accessors (Getters) and mutators (Setters) for fields.
- When applied at class level, generates for all fields; when applied at field level, generates only for that specific field.
2) Constructor Annotations
- @NoArgsConstructor: Generates a default constructor (Required for JPA entities).
- @AllArgsConstructor: Generates a constructor that takes all fields as parameters.
- @RequiredArgsConstructor: Generates a constructor only for
finalor@NonNullfields (Best for constructor injection).
3) @ToString & @EqualsAndHashCode
- @ToString: Generates a
toString()method. Fields can be excluded using theexcludeattribute. - @EqualsAndHashCode: Generates methods for object equality comparison.
4) @Data / @Value
- @Data: A bundle for mutable data objects, including
@Getter,@Setter,@ToString,@EqualsAndHashCode, and@RequiredArgsConstructor. - @Value: An immutable version of
@Data. The class becomesfinal, fields becomeprivate final, and no setters are generated.
5) @Builder (Builder Pattern)
- Generates code for the Builder pattern, improving readability when creating complex objects.
User user = User.builder().id(1L).name("John").build();
6) @NonNull
- Generates null-check code that throws a
NullPointerExceptionif the marked parameter or field is null.
7) @With (Wither)
- Used to create a new copy of an immutable object with one specific field changed. (
user.withName("Smith"))
8) @UtilityClass
- For utility classes where all methods are static. It makes the class final and its constructor private.
9) @Cleanup
- Ensures a resource is automatically closed when the current scope ends (similar to try-with-resources).
10) @SneakyThrows
- Allows you to throw checked exceptions without declaring them in the method signature.
11) @Slf4j / @Log
- Automatically creates a
logobject for logging.@Slf4jis the most widely used in professional projects.
3. How It Works
Lombok analyzes Java source code during compilation and inserts the corresponding methods directly into the bytecode (.class). While not visible in the source code, the compiled file contains the full implementation.
4. 🎯 Key Points
- Lombok drastically improves productivity by reducing boilerplate code.
- @RequiredArgsConstructor is the preferred choice for Spring's constructor injection.
- While starting with
@Datais easy, moving towards fine-grained annotations is often recommended for better control.