java - Inject bean at runtime reading properties file -
suppose have class mailconsoleservice
, class mailsmtpservice
, both implement mailservice
interface. have class emailjob
loads users db , send email through mailservice instance injected spring.
how read properties , determine @ runtime implementation of mailservice
inject? properties change @ time app running, obviously.
i've thought create factory bean returns right instance spring container emailjob
don't know how implement this.
note: beans configured singleton scope, guess i'll have change prototype emailjob
@ least.
note 2: in factory bean how avoid read properties file each time?
thanks!
i not sure understood question. based on understood, if bean @ runtime properties file , file changed @ runtime, below 1 way of doing this. need handle app context , bean name properties file.
the prototype scope has nothing this. if declare bean of type prototype means new bean instance everytime ask app context it.
@component public class emailjob { @autowired private applicationcontext appcontext; public void sendemail(){ mailsender mailsender=(mailsender)appcontext.getbean(<get bean name properties file>); // remaining things } }
Comments
Post a Comment