Master Lombok: Powerful Annotations (@Constructor, @Delegate, @Cleanup, @Builder) Explained
This article showcases clever Lombok tricks—including @onConstructor, @Delegate, @Cleanup, @Singular, @Builder, and @With—demonstrating how they simplify Java code, improve readability, and integrate with Spring for dependency injection while warning against overuse.
Preface
This article shares clever Lombok tricks without judging right or wrong, aiming to inspire readers and provide useful code snippets.
@onX
Examples of using onConstructor, onMethod, and onParam to inject custom annotations, such as combining with Spring's @Autowired.
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyService {
private final AnotherService anotherService;
}Lombok generates a constructor annotated with @Autowired, allowing Spring to automatically inject the required AnotherService bean.
@Delegate
@Delegatelets a class use methods of another class without writing boilerplate, reducing deep inheritance and tight coupling.
// Class A with a method
public class A {
public void sayHello() {
System.out.println("Hello");
}
}
// Class B delegates A
public class B {
@Delegate private A a = new A();
public static void main(String[] args) {
B b = new B();
b.sayHello(); // Calls A's method
}
}@Cleanup
@Cleanupautomatically manages resources, ensuring that close() or a custom release method is called safely.
@Cleanup InputStream in = new FileInputStream("some/file");
@Cleanup("release") MyResource resource = new MyResource();Lombok wraps the code in a try‑finally block and invokes in.close() or resource.release() accordingly.
@Singular and @Builder
@Builderenables fluent object construction, while @Singular simplifies handling of collection fields, allowing both single-element and bulk additions.
@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();After build(), the collection becomes immutable; clearHobbies() can reset it if needed.
@With
@Withcreates a new instance with modified fields, preserving immutability of the original object.
@With
public class Person {
private String name;
private int age;
}
Person person = new Person("Alice", 30);
Person updatedPerson = person.withAge(31);Conclusion
While Lombok provides many convenient features, overusing or misusing them can make code hard to understand and maintain; therefore, apply these annotations judiciously and consider their impact on readability.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
