Project Lombok is a Java library that reduces boilerplate code by automatically generating:
toString()equals() and hashCode()It works at compile time using annotations.
Without Lombok:
public class User {
private Long id;
private String name;
public User() {}
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;
}
// equals(), hashCode(), toString() ...
}
With Lombok:
import lombok.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
✅ Cleaner
✅ More readable
✅ Production-friendly
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Product {
private String name;
private double price;
}
@ToString
public class Product {
private String name;
private double price;
}
Product(name=Phone, price=50000.0)
Generates equals() and hashCode().
@NoArgsConstructor
public class User {
private String name;
}
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
}
@Data
public class Employee {
private Long id;
private String name;
}
@Data
@Builder
public class Order {
private Long id;
private String product;
private int quantity;
}
@Value
public class Address {
String city;
String country;
}
@Slf4j
@Service
public class UserService {
public void test() {
log.info("User service called");
}
}
| Feature | Benefit |
|---|---|
| final fields | Immutable dependency |
| Constructor injection | Testable |
| No @Autowired needed | Cleaner code |
❌ Avoid using @Data on JPA entities.
Your Code → Lombok Annotation Processor → Generated Bytecode → JVM
[Source Code]
|
v
[Lombok Annotation Processor]
|
v
[Compiled .class File]
Q: Is Lombok safe in production?
✅ Yes.
Q: Does Lombok affect runtime performance?
❌ No.
Lombok reduces boilerplate and improves readability in modern Spring Boot development.