Java How use Spring Autowired in SystemInitializer class -
i have java project spring mvc. need start timertasks after application initialized, implemented webapplicationinitializer interface , call systeminitializer. inside class have @autowired property, @autowired property dao class. need cause want execute tasks based in recordings data base. autowired property ever null.
public class systeminitializer implements webapplicationinitializer { @autowired private domainresearchdao domainresearchdao; @override public void run() { if (this.domainresearchdao != null) { system.out.println("ok"); } // here else{ system.out.println("no ok"); } }
you can not use @autowired inside of webapplicationinitializer.
your beans not ready (not scanned yet) injected. application has no idea domainresearchdao @ moment.
spring can autowire beans after application initialized , (singletone) instances (@component, @service etc.) created.
if want job after application started, use spring event doing this:
@component public class doonstart{ @autowired private iyourservice service; @eventlistener public void handlecontextrefresh(contextrefreshedevent e) { // code } } just implement class, no need autowire it.
Comments
Post a Comment