[英]Lombok with default-constructor
I have a class like this: 我有一个这样的课:
@Service("aSpringService")
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class ServiceImpl implements Service {
@NonNull
private final Member1 m1;
@NonNull
private final Member2 m2;
@NonNull
private final Member3 m3;
}
The constructor will be created by Lombok and at runtime, spring will inject the members into the constructor. 构造函数将由Lombok创建,在运行时,spring会将成员注入构造函数。 Now I need a setup-method and got stuck with lombok.
现在我需要一个设置方法,并坚持使用lombok。 It seems, that Lombok cannot call something self-written.
看来,龙目岛不能称之为自编的东西。
I want a new parameter for the Lombok-Annotation like useDefaultConstructor
. 我想要一个像
useDefaultConstructor
这样的Lombok-Annotation的新参数。 When this parameter is present, then the automated-code (from Lombok) will call a parameter-less constructor, which I can write for my own. 当存在此参数时,自动代码(来自Lombok)将调用无参数构造函数,我可以为自己编写。
@RequiredArgsConstructor(onConstructor = @__({@Autowired}), useDefaultConstructor = true)
// Note the "useDefaultConstructor = true"
public class ServiceImpl implements Service {
@NonNull
private final Member1 m1;
private ServiceImpl() {
//some self-written setup-code
}
}
Generated class: 生成的类:
public class ServiceImpl implements Service {
private final Member1 m1;
// This constructor is not generated by lombok
private ServiceImpl() {
//some self-written setup-code
}
// Constructor generated by lombok
@Autowired
public ServiceImpl(Member1 m1) {
this(); // <- only created when "useDefaultConstructor" is present
this.m1 = m1;
}
}
Is there a way to do this with lombok? 有没有办法用lombok做到这一点? Iam to lazy to write the constructor for my own (and change it everytime, when a need a new spring-member).
我懒得为自己编写构造函数(并且每当需要一个新的spring-member时更改它)。
Have you tried Spring @PostConstruct
annotation? 你试过Spring
@PostConstruct
注释吗?
@PostConstruct
public void init() {
// setup-code
}
It is a part of Spring's beans lifecycle management . 它是Spring的bean生命周期管理的一部分 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.