package com.example.demo.model;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.util.List;
// Complete data class with all common methods
@Data
public class Product {
private Long id;
private String name;
private String description;
private double price;
private int stock;
private LocalDateTime createdAt;
}
// Builder pattern
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order {
private Long id;
private Long userId;
private List<OrderItem> items;
@Builder.Default
private String status = "PENDING";
@Builder.Default
private LocalDateTime createdAt = LocalDateTime.now();
}
// Immutable value object
@Value
public class Money {
double amount;
String currency;
public Money add(Money other) {
if (!currency.equals(other.currency)) {
throw new IllegalArgumentException("Currency mismatch");
}
return new Money(amount + other.amount, currency);
}
}
// With method for immutable updates
@Value
@With
public class UserSettings {
String theme;
String language;
boolean notifications;
}
// Required args constructor for dependency injection
@RequiredArgsConstructor
@Slf4j
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentService paymentService;
public Order processOrder(Order order) {
log.info("Processing order: {}", order.getId());
try {
paymentService.charge(order);
order.setStatus("PAID");
log.debug("Payment successful for order: {}", order.getId());
} catch (Exception e) {
log.error("Payment failed for order: {}", order.getId(), e);
order.setStatus("FAILED");
}
return orderRepository.save(order);
}
}
// Getter only (read-only)
@Getter
@AllArgsConstructor
public class ReadOnlyModel {
private final Long id;
private final String name;
}
// Custom toString
@ToString(exclude = "password")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class User {
@EqualsAndHashCode.Include
private Long id;
private String username;
private String password;
private String email;
}
// Cleanup resources automatically
public class ResourceExample {
public void processFile(String path) {
@Cleanup val inputStream = new FileInputStream(path);
// Stream automatically closed
}
}
// Synchronized methods
public class ThreadSafeCounter {
private int count = 0;
@Synchronized
public void increment() {
count++;
}
@Synchronized
public int getCount() {
return count;
}
}
Project Lombok generates common Java code via annotations at compile-time. @Data creates getters, setters, toString, equals, and hashCode. @Builder implements the builder pattern. @Slf4j provides logger instances. @NoArgsConstructor and @AllArgsConstructor generate constructors. @RequiredArgsConstructor creates constructors for final fields. @Value makes immutable classes. @With creates copies with modified fields. Lombok reduces hundreds of lines of boilerplate, improving maintainability. IDE plugins enable code navigation. Configuration via lombok.config customizes behavior. While powerful, I use Lombok judiciously—overuse can obscure code behavior. The tool integrates seamlessly with Spring Boot, making DTOs and entities concise. Lombok enhances developer productivity without runtime dependencies.