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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -