Spring Lombok
In Spring, Lombok is a Java library that is used to minimize boilerplate code. It uses annotations to generate common methods like getters, setters, constructors, toString
, equals
, and hashCode
methods during compile time. Below are some of the most common Lombok annotations and features:
Common Lombok Annotations and Features:
- @NoArgsConstructor: Generates a no-arguments constructor.
- @RequiredArgsConstructor: Generates a constructor with required arguments (fields marked as
final
or annotated with@NonNull
). - @AllArgsConstructor: Generates a constructor with all fields as arguments.
- @Data: Generates
getter
,setter
,equals()
,hashCode()
, andtoString()
methods. - @Value: Similar to
@Data
, but generates an immutable class (all fields arefinal
). - @Builder: Generates a builder pattern for creating complex objects.
- @SneakyThrows: Automatically adds exception handling code in methods.
- @Cleanup: Automatically closes resources, like file streams or database connections.
- @Slf4j: Automatically generates logging code and integrates with various logging frameworks.
- @EqualsAndHashCode: Generates
equals()
andhashCode()
methods. - @ToString: Generates a
toString()
method. - @Getter: Generates
getter
methods. - @Setter: Generates
setter
methods. - @Delegate: Delegates method implementation to another object.
- @Value.Immutable: Generates immutable value objects.
- @Wither: Generates a method that returns a new instance with a specified field value changed.
- @NoArgsConstructor(force = true): Generates a no-arguments constructor, even if there are
final
fields.
Detailed Explanation and Usage Examples:
@NoArgsConstructor, @AllArgsConstructor, and @RequiredArgsConstructor:
@NoArgsConstructor
: Generates a no-arguments constructor.@AllArgsConstructor
: Generates a constructor with all fields as arguments.@RequiredArgsConstructor
: Generates a constructor with required arguments (fields marked asfinal
or annotated with@NonNull
).
1
2
3
4
5
6
7
8
9
10import lombok.*;
public class Person {
private String firstName;
private String lastName;
private int age;
}@Data and @Value:
@Data
: Generatesgetter
,setter
,equals()
,hashCode()
, andtoString()
methods.@Value
: Similar to@Data
, but generates an immutable class (all fields arefinal
).
1
2
3
4
5
6
7
8import lombok.*;
public class Book {
private String title;
private String author;
private int pageCount;
}@Builder: Generates a builder pattern for creating complex objects.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import lombok.*;
public class Product {
private String name;
private String category;
private double price;
}
// Creating an instance using the builder pattern
Product product = Product.builder()
.name("Widget")
.category("Electronics")
.price(19.99)
.build();@SneakyThrows: Automatically adds exception handling code in methods.
1
2
3
4
5
6
7
8
9import lombok.SneakyThrows;
public class FileProcessor {
public void processFile() {
// No need to explicitly handle the exception
throw new Exception("Something went wrong.");
}
}@Cleanup: Automatically closes resources, like file streams or database connections.
1
2
3
4
5
6
7
8import lombok.Cleanup;
public class FileHandler {
public void readFile(String filePath) {
FileReader reader = new FileReader(filePath);
// Read file
}
}@Slf4j: Automatically generates logging code and integrates with various logging frameworks.
1
2
3
4
5
6
7
8
9import lombok.extern.slf4j.Slf4j;
public class LoggerExample {
public void logSomething() {
log.debug("This is a debug message.");
log.info("This is an info message.");
}
}@Getter and @Setter: Generates
getter
andsetter
methods.1
2
3
4
5
6
7import lombok.Getter;
import lombok.Setter;
public class Student {
private String name;
private int age;
}
Using Lombok annotations can significantly reduce the amount of boilerplate code in your Java classes, making your code more concise and easier to maintain.