java - Spring autowire a class on server startup -
i have spring application. autowiring classes , working fine. e.g
@controller public class searchcontroller { @autowired private environmentcontrol envcontrol; @autowired private searchcontrol searchcontrol; ...
but have on server startup class called scheduleservlet uses init method schedule something...
public class schedulerservlet extends httpservlet { @override public void init(servletconfig config) throws servletexception { super.init(config); this.logger.info("timer servlet initialized "); try { initialcontext ic = new initialcontext(); timermanager tm = (timermanager) ic.lookup("java:comp/env/tm/timermanager"); timer timer = tm.schedule(new globaltemplatescheduler(), 0, 3600000);// 1 hour interval system.out.println("timer..... " + timer); } ...
in globaltemplatescheduler class has timerexpired method scheduled execute after every 1 hour interval.
public class globaltemplatescheduler implements timerlistener { @autowired private templatecontrol templatecontrol; @override public void timerexpired(timer timer) { try { templatecontrol.updatemappings(names); } catch (exception e) { this.logger.error(e.getmessage()); e.printstacktrace(); } ...
so have autowire templatecontrol getting null. should happen on server startup.
further inside updatemappings there's datasource object autowired constructor-arg(this working fine on browser request need on server startup).
note: cannot use applicationlistener interface.
any suggestions help.
thankyou.
on application startup beans initialization not completed, beans can used after application context refresh or after intialization of bean, make no sense execute logic requires bean on startup unless detect whether bean ready or not.
you can execute logic using @postconstruct in bean executed after initialization of bean can manipulate logic in way after intialization of bean or detect , execute logic after contextrefreshedevent impelementing applicationlistener , put logic in onappplicationevent method.
Comments
Post a Comment