Pros and Cons of Using Lombok in Java Development
This article examines Lombok's ability to auto‑generate boilerplate code such as getters, setters, toString, and equals, demonstrates its usage with Maven dependencies and annotated examples, and discusses its advantages and drawbacks—including IDE reliance, JDK compatibility, and debugging challenges—before concluding with a recommendation for its selective use.
Lombok is a Java library that can automatically generate common methods like setter, getter, toString, and equals, reducing boilerplate code and improving development efficiency.
To use Lombok, add the following Maven dependency:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>A simple class with Lombok can be written as:
import lombok.Data;
@Data
public class User {
private String id;
private String name;
private String desc;
}Lombok provides many useful annotations, such as:
@NonNull – null‑pointer check
@Cleanup – automatic resource closing
@Getter/@Setter – generate getters and setters
@ToString – generate toString method
@EqualsAndHashCode – generate equals and hashCode
@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor – constructors
@Data – combines @ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor
@Builder – builder pattern
@SneakyThrows – suppress checked exceptions
@Synchronized – synchronized method with automatic lock
@Log and related annotations – logging utilities
Example with multiple Lombok annotations and a main method:
import lombok.*;
import java.io.FileInputStream;
import java.io.InputStream;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
private String id;
private String name;
private String desc;
public static void main(String[] args) {
new User();
new User("juice-resume", "果汁简历", "专注于 Java 方向技术分享");
User user = User.builder().id("juice-resume").name("果汁简历").desc("专注于 Java 方向技术分享").build();
}
@SneakyThrows
private void read() {
@Cleanup InputStream inputStream = new FileInputStream("");
}
@Synchronized
public void write() {
System.out.println("write");
}
}While Lombok offers clear benefits, it also has drawbacks:
1. IDE Dependency
Using Lombok requires installing a plugin in IDEs like IntelliJ IDEA, adding extra setup for new developers.
2. JDK Compatibility
Some Lombok versions may not be compatible with newer JDK releases (e.g., moving from Java 8 to Java 11), though workarounds like Delombok exist.
3. Debugging Difficulty
Generated methods are not present in source code, making step‑through debugging harder, especially for developers preferring a rich domain model.
Overall, the efficiency gains outweigh the disadvantages for most projects, and Lombok is recommended as a productivity tool rather than a core architectural decision.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
