1

I have superclass:

@AllArgsConstructor
public abstract class AbstractSuperService {
 protected final SuperRepository superRepository;
 
}

And two subclass:

@Service
public class OneSubService extends AbstractSuperService {
 private final OneRepository oneRepository;
 
 public OneSubService(SuperRepository superRepository, OneRepository oneRepository){
 super(superRepository);
 this.oneRepository = oneRepository;
 }
}
@Service
public class SecondSubService extends AbstractSuperService {
 private final SecondRepository secondRepository;
 public SecondSubService(SuperRepository superRepository, SecondRepository secondRepository){
 super(superRepository);
 this.secondRepository = secondRepository;
 }
}

This code is work. But if I change code: delete constructor in subclasses and add annotation lombok @SuperBuilder in all class.

@AllArgsConstructor
@SuperBuilder
public abstract class AbstractSuperService {
 protected final SuperRepository superRepository;
 
}
@Service
@SuperBuilder
public class OneSubService extends AbstractSuperService {
 private final OneRepository oneRepository;
 
}
@Service
@SuperBuilder
public class SecondSubService extends AbstractSuperService {
 private final SecondRepository secondRepository;
}

The following error appears:

Description:
Parameter 0 of constructor in OneSubService required a bean of type 'OneSubService$OneSubServiceBuilder' that could not be found.
Action:
Consider defining a bean of type 'OneSubService$OneSubServiceBuilder' in your configuration.

How to correctly apply the lombok annotation in subclass, so as not to write constructor in subclass?

asked Jun 16, 2022 at 13:47

1 Answer 1

2

If you use @SuperBuilder, no constructor is created. In Spring in order to inject OneRepository and SuperRepository, you need a constructor having these two objects as parameters.

Using @AllArgConstructor, Lombok creates the constructor just for the members of that class, not considering the super class. Why? You can read the answer of a Lombok developer here.

In the end, your first solution is a unique solution if you want to have a super class.

answered Jun 16, 2022 at 13:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.