Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any "PostConstruct" feature of lombok?

Tags:

java

lombok

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();
like image 376
kuang Avatar asked Dec 15 '16 03:12

kuang


People also ask

Does Lombok data create constructor?

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.

Is Lombok builder immutable?

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.

Does Lombok affect performance?

It does not decrease performance at runtime, there is a performance flaw at compiletime.

Is Lombok included in spring boot?

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).


1 Answers

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.

like image 171
maaartinus Avatar answered Sep 28 '22 08:09

maaartinus