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?
CodePudding user response:
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.
CodePudding user response:
Remove the @AllArgsConstructor
from AbstractSuperService