Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 358 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
@Autowired bean is null when referenced in the constructor of another bean

#1
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");
}
Reply

#2
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();
}

Reply

#3
[Autowiring][1] (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`][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
Yes, both answers are correct.

To be honest, this problem is actually similar to the post [Why is my Spring @Autowired field null?][1] .

The root cause of the error can be explained in the Spring reference doc ([Autowired][2]) , as follow:

> **Autowired Fields**
>
> Fields are injected right after construction of a bean, before any
> config methods are invoked.

But the real reason behind this statement in Spring doc is the **Lifecycle of Bean** in Spring. This is part of Spring's design philosophy.

This is [Spring Bean Lifecycle Overview][3]:
[![enter image description here][4]][4]
Bean needs to be initialized first before it can be injected with properties such as field. This is how beans are designed, so this is the real reason.

I hope this answer is helpful to you!


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through