Skip to content
Spring Boot sb lombok 4 min read

Introduction to Lombok

Project Lombok is a Java library that eliminates repetitive boilerplate code by generating it for you at compile time. Instead of hand-writing getters, setters, constructors, equals, hashCode, toString, and logging fields, you add a single annotation and Lombok injects the methods directly into the compiled bytecode. It is one of the most widely used libraries in the Spring Boot ecosystem.

The boilerplate problem

A plain Java data class quickly fills with mechanical, error-prone code. Consider a simple domain object:

public class User {
    private Long id;
    private String name;
    private String email;

    public User() {}

    public User(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    @Override
    public boolean equals(Object o) { /* ... */ }
    @Override
    public int hashCode() { /* ... */ }
    @Override
    public String toString() { /* ... */ }
}

Most of this file is noise that hides the three fields that actually matter. With Lombok the same class collapses to:

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private String email;
}

Lombok generates the getters, setters, equals, hashCode, toString, and a required-args constructor during compilation. Your source stays focused on intent, and there is less code to read, review, and keep in sync.

Adding the Maven dependency

Lombok is a compile-time dependency, so mark it optional (or use the provided scope). It is not needed at runtime because the generated methods are already baked into your .class files.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

When you generate a project from Spring Initializr, selecting Lombok adds exactly this dependency. The version is managed by the spring-boot-starter-parent, so you usually do not pin it yourself.

For Gradle, declare it as compileOnly plus annotationProcessor:

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

Note: Lombok belongs to compile time only. It does not ship in your application JAR and adds zero runtime overhead.

IDE and annotation-processing setup

Because Lombok generates methods that do not exist in your source, your IDE must understand them or it will flag user.getName() as an error even though compilation succeeds.

  • IntelliJ IDEA: Lombok support is built in since 2020.3. Ensure Settings → Build, Execution, Deployment → Compiler → Annotation Processors → Enable annotation processing is checked.
  • Eclipse / STS: Run the Lombok JAR (java -jar lombok.jar) once to patch the IDE, then restart.
  • VS Code: Install the Lombok Annotations Support extension (or rely on the bundled support in the Java extension pack).

Maven and Gradle compile Lombok correctly with no plugin once the dependency is present, so a command-line build works even when the IDE is not configured.

How Lombok works at compile time

Lombok is an annotation processor that hooks into the compiler. During the javac compilation phase it inspects your annotated classes and modifies the Abstract Syntax Tree (AST) in place, adding the requested methods before bytecode is emitted.

StageWhat happens
Parsejavac builds the AST from your .java source
Annotation processingLombok mutates the AST, injecting methods/fields
Bytecode generationjavac emits .class files containing the new members
RuntimeGenerated methods behave like hand-written ones

This is why the methods appear in the compiled class and in stack traces, yet never clutter your source file. Lombok relies on Java’s standard annotation-processing API (see Java Annotations), so it integrates with any compliant toolchain.

Tip: If you ever need to see or commit the generated source — for example to drop Lombok from a module — use delombok, covered in Lombok Best Practices.

In This Section

Last updated June 13, 2026
Was this helpful?