Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Shown below is a snippet of code where I try and reference my ApplicationProperties bean. When I reference it from the constructor it is null, but when referenced from another method it is fine. Up until now I have not had no problem using this autowired bean in other classes. But this is the first time I have tried to use it in the constructor of another class.

In the code snippet below applicationProperties is null when called from the constructor but when referenced in the convert method it is not. What am I 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 I try and use the Autowired applicationProperties bean in the constructor   // it is 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 is s snippet from ApplicationProperties ...

@Component public class ApplicationProperties implements IApplicationProperties {    /* Use the appProperties bean defined in WEB-INF/applicationContext.xml    * which 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");   } 
like image 651
hairyone Avatar asked Jun 13 '11 20:06

hairyone


People also ask

Why my Autowired Bean is null?

The field annotated @Autowired is null because Spring doesn't know about the copy of MileageFeeCalculator that you created with new and didn't know to autowire it.

Can we use @autowired on constructor?

You can apply @Autowired to constructors as well. A constructor @Autowired annotation indicates that the constructor should be autowired when creating the bean, even if no <constructor-arg> elements are used while configuring the bean in XML file.

Why is @autowired not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

Is Autowired annotation required on constructor?

Is @Autowired annotation mandatory for a constructor? No. After Spring 4.3 If your class has only single constructor then there is no need to put @Autowired .


2 Answers

Autowiring (link from Dunes comment) happens after the construction of an object. Therefore they will not be set until after the constructor has completed.

If you need to run some initialization code, you should be able to pull the code in the constructor into a method, and annotate that method with @PostConstruct.

like image 95
nicholas.hauschild Avatar answered Oct 05 '22 03:10

nicholas.hauschild


To have dependencies injected at construction time you need to have your constructor marked with the @Autowired annoation like so.

@Autowired public DocumentManager(IApplicationProperties applicationProperties) {   this.applicationProperties = applicationProperties;   startOOServer(); } 
like image 42
mR_fr0g Avatar answered Oct 05 '22 04:10

mR_fr0g