Suppose I have a class MailConsoleService and a class MailSMTPService, both implement the MailService interface. I have a class EmailJob which loads the users from a db and send an email through a MailService instance injected by Spring.
How could I read a properties and determine at runtime which implementation of MailService to inject? The properties could change at any time the app is running, obviously.
I've thought about to create a factory bean which returns the right instance from the spring container to EmailJob but I don't know how to implement this.
Note: All my beans are configured to Singleton scope, so I guess I'll have to change to Prototype EmailJob at least.
Note 2: In the factory bean how could I avoid to read the properties file each time?
Thanks!