我正在开发一个涉及Spring框架的新软件组件。我很喜欢它,但现在我有一个关于IoC和序列化的问题。
假设我有这个类(省略了导入和包声明):
public class EMailNotificationEndpoint implements NotificationEndpoint {
private List<String> notficationEmailAdresses = new ArrayList<String>();
transient private MailSender mailSender;
public EMailNotificationEndpoint(MailSender mailSender) {
this.mailSender = mailSender;
}
@Override
public void notify(Notification notification) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(notficationEmailAdresses.toArray(new String[notficationEmailAdresses.size()]));
simpleMailMessage.setSubject(notification.getType().toString());
simpleMailMessage.setText(notification.getMessage());
mailSender.send(simpleMailMessage);
}
}
NotificationEndpoint扩展了Serializable接口,以确保所有实现都是可序列化的。关键在于我无法将MailSender(它是org.springframework.mail.MailSender)序列化,并希望在反序列化时注入它。现在我的问题很明显:我能做到吗?如果答案是肯定的:怎么做?
我可以自由更改接口和实现,因此应该有一种方法来实现这样的事情,即使这意味着我必须重构相关的类。
非常感谢任何想法或提示!
mailSender
? - Nick Holt