1

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. Now I need a setup-method and got stuck with lombok. It seems, that Lombok cannot call something self-written.

What I want

I want a new parameter for the Lombok-Annotation like useDefaultConstructor. When this parameter is present, then the automated-code (from Lombok) will call a parameter-less constructor, which I can write for my own.

@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;
    }
}

The question

Is there a way to do this with lombok? Iam to lazy to write the constructor for my own (and change it everytime, when a need a new spring-member).

0

1 Answer 1

5

Have you tried Spring @PostConstruct annotation?

@PostConstruct
public void init() {
    // setup-code
}

It is a part of Spring's beans lifecycle management.

Sign up to request clarification or add additional context in comments.

2 Comments

Thats exactly what I want to do.
You can find more about Beans Lifecycle in this chapter of Spring Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.