Using Lombok Annotations @RequiredArgsConstructor, @Delegate, @Cleanup, @Singular and @Builder in Spring Applications
This article demonstrates how Lombok annotations such as @RequiredArgsConstructor, @Delegate, @Cleanup, @Singular, and @Builder can be combined with Spring components to reduce boilerplate, automatically inject dependencies, manage resources, and build immutable objects, providing practical code examples and usage guidelines.
The article introduces several Lombok annotations that can simplify Java code, especially when used together with Spring framework components.
@RequiredArgsConstructor (onConstructor = @__(@Autowired)) allows Lombok to generate a constructor that is automatically annotated with @Autowired , enabling Spring to inject required beans without writing boilerplate code.
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyService {
private final AnotherService anotherService;
}Lombok expands the above to a class containing a constructor annotated with @Autowired , which Spring uses for dependency injection.
@Service
public class MyService {
private final AnotherService anotherService;
@Autowired
public MyService(AnotherService anotherService) {
this.anotherService = anotherService;
}
}@Delegate lets a class expose the methods of another class without manually delegating each method. By annotating a field with @Delegate , Lombok generates forwarding methods.
// A class with a method
public class A {
public void sayHello() {
System.out.println("Hello");
}
}
// B delegates A's methods
public class B {
@Delegate
private A a = new A();
public static void main(String[] args) {
B b = new B();
b.sayHello(); // calls A.sayHello()
}
}@Cleanup automatically wraps a resource in a try‑finally block and calls the specified cleanup method (default close ) when the scope ends.
@Cleanup InputStream in = new FileInputStream("some/file");For resources with a different release method, the method name can be supplied:
@Cleanup("release") MyResource resource = new MyResource();@Builder together with @Singular enables fluent, type‑safe construction of objects, especially when the class contains collection fields. @Singular generates methods to add single elements or whole collections, and the built collection becomes immutable.
@Data
@Builder
public class User {
private String name;
private int age;
@Singular
private List
hobbies;
}
User user = User.builder()
.name("练习时长两年半")
.age(28)
.hobby("篮球")
.hobby("唱歌")
.hobbies(Arrays.asList("跳舞", "其他"))
.build();The article concludes by warning that while Lombok greatly reduces boilerplate, excessive or inappropriate use can make code harder to understand and maintain, so developers should apply it judiciously.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.