Advanced Lombok Annotations for Simplifying Java Backend Code
This article demonstrates how Lombok’s advanced annotations such as @RequiredArgsConstructor(onConstructor=@__(@Autowired)), @Delegate, @Cleanup, and the combination of @Builder with @Singular can automatically generate Spring‑compatible constructors, delegate methods, manage resources, and build immutable objects, thereby reducing boilerplate and improving code readability in Java backend development.
Introduction
The article does not discuss right or wrong; it simply shares clever tricks that can be useful in Java development. Readers are encouraged to explore the techniques and decide for themselves whether to apply them in real projects.
Using Lombok to inject Spring beans
Lombok’s @RequiredArgsConstructor(onConstructor = @__(@Autowired)) allows you to add the @Autowired annotation to the constructor generated by Lombok, enabling Spring to inject required dependencies automatically.
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyService {
private final AnotherService anotherService;
}The generated code becomes:
@Service
public class MyService {
private final AnotherService anotherService;
@Autowired
public MyService(AnotherService anotherService) {
this.anotherService = anotherService;
}
}This constructor receives an AnotherService instance, and because it is annotated with @Autowired , Spring will automatically locate and inject the appropriate bean.
@Delegate
The @Delegate annotation lets a class expose methods of another class without writing forwarding code.
// A simple 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()
}
}This reduces deep inheritance hierarchies and tight coupling, improving readability and maintainability.
@Cleanup
@Cleanup automatically manages resources that need to be closed, such as streams. It wraps the resource in a try‑finally block and calls the specified close method.
@Cleanup InputStream in = new FileInputStream("some/file");When the code finishes, Lombok inserts in.close() . If the resource uses a different release method, you can specify it:
@Cleanup("release") MyResource resource = new MyResource();Lombok will then call resource.release() in the finally block.
@Builder and @Singular
@Builder enables fluent, chainable object construction, while @Singular makes collection fields easier to populate.
@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 generated collection becomes immutable after build() , ensuring thread safety. You can also clear the collection with clearHobbies() before adding new elements.
Conclusion
While Lombok provides many convenient features, excessive or improper use can make code harder to understand and maintain. Use these annotations judiciously and always consider their impact on code clarity.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.