Lombok Integration
Same masala, no grinding. Let Lombok do the boilerplate for you.
The Hyderabad Spice Packet Analogy:
Making a traditional Hyderabadi Biryani from scratch means buying whole spices, grinding them, and roasting them mdash; taking hours. But if you buy a ready-made spice packet from the store, you get the exact same masala instantly without the hard work.
In Java, Getters, Setters, and Constructors are the “grinding spices” process. Lombok is the spice packet. You just put an annotation like @Data on your class, and Lombok secretly injects all the getters and setters into the compiled bytecode.
- @Data: A shortcut that bundles
@Getter,@Setter,@ToString, and@EqualsAndHashCode. - @Builder: Allows you to create objects using the Builder design pattern (e.g.,
Book.builder().title("X").build()). - @Slf4j: Automatically injects a logger variable named
loginto your class so you don’t have to writeLoggerFactory.getLogger(…).
To use Lombok, you must add it to your pom.xml.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>package com.devinhyderabad;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
// 1. @Data generates getters, setters, toString, equals, hashCode
// 2. @NoArgsConstructor is required by JPA/Hibernate
// 3. @AllArgsConstructor is useful for manual object creation
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
// Look! No getters, no setters, no constructors written manually.
}package com.devinhyderabad;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j // Injects the ’log’ variable automatically
public class BookService {
public void createBook() {
// Using the Lombok Builder pattern
Book book = Book.builder()
.title("Spring Boot Guide")
.author("Deva")
.build();
// Using the injected logger
log.info("Created book with title: {}", book.getTitle());
}
}Interview Question: “How does Lombok actually work under the hood? Does it slow down runtime?”
Answer: Lombok works at compile-time, not runtime. It uses the Java Annotation Processing API (JSR 269) to hook into the compiler. When you compile your code, Lombok modifies the Abstract Syntax Tree (AST) and injects the getter/setter bytecode directly into the .class file. Because of this, there is zero runtime performance overhead.
Enterprise Note: While @Data is great for DTOs, be careful using it on JPA @Entity classes. @Data generates equals() and hashCode() based on all fields, which can break Hibernate’s lazy loading if it triggers database queries. For Entities, prefer @Getter and @Setter instead of @Data.
Key Takeaways
- ✅ Lombok generates getters, setters, constructors at compile-time — zero runtime overhead
- ✅ @Data bundles @Getter, @Setter, @ToString, @EqualsAndHashCode
- ✅ @Builder enables the Builder design pattern for cleaner object creation
- ✅ @Slf4j auto-injects a logger variable into your class
- ✅ Prefer @Getter/@Setter over @Data for JPA Entities to avoid lazy-loading issues
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login