抽象类和子级上的构造函数注入
我有一个班级如下
@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
是 一个,并且应该通过构造函数注入自动连接。@Service
现在我有一个类,可以扩展并且还应该自动连接组件NotificationCenter
@Service
public class NotificationCenterA extends NotificationCenter {
private final TemplateBuildingService templateBuildingService;
public NotificationCenterA(TemplateBuildingService templateBuildingService) {
this.templateBuildingService = templateBuildingService;
}
}
根据上面的示例,代码将无法编译,因为抽象类中没有默认构造函数,除非我添加为构造函数的第一个语句,但我没有的实例,并且我不打算从子级填充基字段。NotificationCenter
super(emailService);
NotificationCenterA
emailService
任何想法处理这种情况的正确方法是什么?也许我应该使用现场注入?