I'm just in the middle of developing a new software component using the Spring Framework. I like it, but now I have a question regarding IoC and serialization.
Given that I have this class (excluded imports and package declaration):
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 extends Serializable to enable serialization of all implementations. The thing is, I cannot serialize MailSender (which is org.springframework.mail.MailSender BTW) and wants it to be injected during deserialization. Now my question is obvious: can I do this? And if the answer is yes: how?
I can change the interface and implementations, so there must be a way to such a substance, even if it means that I have to reorganize the corresponding classes.
Any idea or hint is much appreciated!
Malax source share