Lombok Tricks: Using @onX, @Delegate, @Cleanup, @Singular/@Builder, and @With

This article showcases several handy Lombok annotations—@onX for custom constructor injection, @Delegate for method delegation, @Cleanup for automatic resource release, the @Singular/@Builder combo for fluent object creation, and @With for immutable copies—illustrating their usage with Spring and Java code examples while warning against overuse.

Programmer XiaoFu
Programmer XiaoFu
Programmer XiaoFu
Lombok Tricks: Using @onX, @Delegate, @Cleanup, @Singular/@Builder, and @With

@onX

The @onX family (onConstructor, onMethod, onParam) lets you inject custom annotations into Lombok‑generated code. A common use is combining it with Spring's @Autowired to have Lombok generate a constructor that Spring can automatically inject.

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyService {
    private final AnotherService anotherService;
}

Lombok expands the above to:

@Service
public class MyService {
    private final AnotherService anotherService;

    @Autowired
    public MyService(AnotherService anotherService) {
        this.anotherService = anotherService;
    }
}

Generates a constructor that accepts an AnotherService instance.

Because the constructor is annotated with @Autowired, Spring injects the appropriate bean automatically.

@Delegate

@Delegate allows a class to expose the methods of another class without writing forwarding code, reducing boilerplate and avoiding deep inheritance hierarchies.

public class A {
    public void sayHello() {
        System.out.println("Hello");
    }
}

public class B {
    @Delegate
    private A a = new A();
}

Now b.sayHello() directly calls A.sayHello().

@Cleanup

@Cleanup automatically wraps a resource declaration in a try‑finally block and calls the specified cleanup method (default close) when the scope ends.

@Cleanup InputStream in = new FileInputStream("some/file");

Lombok generates code that calls in.close() in the finally block.

@Cleanup("release") MyResource resource = new MyResource();

This will invoke resource.release() for cleanup.

@Singular and @Builder

@Builder enables fluent object construction, while @Singular adds convenient methods for collection fields, allowing both single‑element and bulk addition.

@Data
@Builder
public class User {
    private String name;
    private int age;
    @Singular
    private List<String> hobbies;
}

User user = User.builder()
    .name("练习时长两年半")
    .age(28)
    .hobby("篮球")
    .hobby("唱歌")
    .hobbies(Arrays.asList("跳舞", "其他"))
    .build();

@Singular generates hobby(String) and hobbies(Collection) methods; the built collection becomes immutable. Note that if the class extends a parent, @Builder only includes the child’s fields.

@With

@With creates a new instance that is a copy of the original with selected fields changed, supporting immutable‑style updates.

@With
public class Person {
    private String name;
    private int age;
}

Person person = new Person("Alice", 30);
Person updated = person.withAge(31);

Conclusion

While Lombok provides powerful shortcuts that can make Java code more concise, excessive or inappropriate use may obscure intent and hinder maintainability; therefore, apply these annotations judiciously and ensure the team understands their effects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaCode GenerationSpringAnnotationsLombok
Programmer XiaoFu
Written by

Programmer XiaoFu

xiaofucode.com – a programmer learning guide driven by the pursuit of profit

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.