Advanced Lombok Annotations: @onX, @Delegate, @Cleanup, @Builder/@Singular, and @With
This article explains several powerful Lombok annotations—including @onX, @Delegate, @Cleanup, @Builder with @Singular, and @With—showing how they simplify Spring‑based Java code through automatic constructor generation, method delegation, resource management, and fluent builders, while also warning against overuse.
The article introduces a series of Lombok annotations that enable concise and expressive Java code, especially when used together with Spring components.
@onX allows custom annotations to be injected into generated code, such as onConstructor , oMethod , and onParam . A typical use case is combining it with Spring's @Autowired :
@Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class MyService { private final AnotherService anotherService; }
Lombok then generates a constructor annotated with @Autowired , enabling Spring to inject the required AnotherService bean automatically.
@Delegate lets a class expose methods of another class without writing boilerplate delegation code. Example:
public class A { public void sayHello() { System.out.println("Hello"); } } public class B { @Delegate // delegates A's methods private A a = new A(); public static void main(String[] args) { B b = new B(); b.sayHello(); // calls A.sayHello() } }
This reduces deep inheritance hierarchies and tight coupling, improving readability and maintainability.
@Cleanup automatically manages resources that need to be closed. Simple usage:
@Cleanup InputStream in = new FileInputStream("some/file");
Lombok wraps the code in a try‑finally block and calls in.close() . A custom cleanup method can be specified:
@Cleanup("release") MyResource resource = new MyResource();
Here Lombok will invoke resource.release() in the finally clause.
@Builder together with @Singular provides fluent builders and convenient handling of collection fields. Example class:
@Data @Builder public class User { private String name; private int age; @Singular private List hobbies; }
Using the builder:
User user = User.builder() .name("练习时长两年半") .age(28) .hobby("篮球") .hobby("唱歌") .hobbies(Arrays.asList("跳舞", "其他")) .build();
The @Singular annotation generates methods to add single elements ( hobby() ) and whole collections ( hobbies() ), and the resulting collection becomes immutable after build() . The builder can also clear a collection:
User user = User.builder() .name("签") .age(28) .hobby("说唱") .hobby("跳舞") .clearHobbies() .hobby("踩缝纫机") .build();
Note that if the class extends a parent, @Builder only includes fields declared in the subclass.
@With creates a copy of an object with modified fields, supporting immutable patterns. Example:
@With public class Person { private String name; private int age; } Person person = new Person("Alice", 30); Person updatedPerson = person.withAge(31);
The article concludes by cautioning that while Lombok greatly reduces boilerplate, excessive or inappropriate use can make code harder to understand and maintain, so developers should apply these annotations judiciously.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.