I have a class as follows
@Component
public abstract class NotificationCenter {
protected final EmailService emailService;
protected final Logger log = LoggerFactory.getLogger(getClass());
protected NotificationCenter(EmailService emailService) {
this.emailService = emailService;
}
protected void notifyOverEmail(String email, String message) {
//do some work
emailService.send(email, message);
}
}
EmailService
is a @Service
and should be auto-wired by constructor injection.
Now I have a class that extends NotificationCenter
and should also auto-wire components
@Service
public class NotificationCenterA extends NotificationCenter {
private final TemplateBuildingService templateBuildingService;
public NotificationCenterA(TemplateBuildingService templateBuildingService) {
this.templateBuildingService = templateBuildingService;
}
}
Based on the above example the code won't compile because there is no default constructor in the abstract class NotificationCenter
unless I add super(emailService);
as the first statement to NotificationCenterA
constructor but I don't have an instance of the emailService
and I don't intend to populate the base field from children.
Any idea what's the proper way to handle this situation? Maybe I should use field injection?