java - @Autowired bean is null when referenced in the constructor of another bean -


shown below snippet of code try , reference applicationproperties bean. when reference constructor null, when referenced method fine. until have not had no problem using autowired bean in other classes. first time have tried use in constructor of class.

in code snippet below applicationproperties null when called constructor when referenced in convert method not. missing

@component public class documentmanager implements idocumentmanager {    private log logger = logfactory.getlog(this.getclass());   private officemanager officemanager = null;   private converterservice converterservice = null;    @autowired   private iapplicationproperties applicationproperties;     // if try , use autowired applicationproperties bean in constructor   // null ?    public documentmanager() {   startooserver();   }    private void startooserver() {     if (applicationproperties != null) {       if (applicationproperties.getstartooserver()) {         try {           if (this.officemanager == null) {             this.officemanager = new defaultofficemanagerconfiguration()               .buildofficemanager();             this.officemanager.start();             this.converterservice = new converterservice(this.officemanager);           }         } catch (throwable e){           logger.error(e);           }       }     }   }    public byte[] convert(byte[] inputdata, string sourceextension, string targetextension) {     byte[] result = null;      startooserver();     ... 

below s snippet applicationproperties ...

@component public class applicationproperties implements iapplicationproperties {    /* use appproperties bean defined in web-inf/applicationcontext.xml    * in turn uses resources/server.properties    */   @resource(name="appproperties")   private properties appproperties;    public boolean getstartooserver() {     string val = appproperties.getproperty("startooserver", "false");     if( val == null ) return false;     val = val.trim();     return val.equalsignorecase("true") || val.equalsignorecase("on") || val.equalsignorecase("yes");   } 

autowiring (link dunes comment) happens after construction of object. therefore not set until after constructor has completed.

if need run initialization code, should able pull code in constructor method, , annotate method @postconstruct.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -