Is there a way that I can define a "PostConstruct" initialization method with lombok?
@RequiredArgsConstructor(staticName = "of")
class MyObj {
private final int x;
private final int y;
private int z;
// not work
@PostConstruct
private void someInitLogic {
z = x + y;
}
public void start() {
// code use "z"
}
}
So that I can init an object like:
MyObj obj = MyObj.of(1, 2);
obj.start();
Lombok Data annotation ( @Data ) Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
Be aware that Lombok is not an immutability library but a code generation library which means some setups might not create immutable objects. For example, Lombok's @Data is equivalent to Lombok's @Value but will also synthesize mutable methods. Don't use Lombok's @Data when creating immutable classes.
It does not decrease performance at runtime, there is a performance flaw at compiletime.
Lombok Dependency Note: If you're using a Spring Boot POM, Project Lombok is a curated dependency. Thus, you can omit the version (which will then be inherited from the Spring Boot parent POM).
Not yet. There's an open issue named just like your question. Unfortunately, Lombok development is rather slow and there are many feature requests open. Vote for this one (don't add "+1", use the button), if you really care.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With