Why is my Spring @Autowired field null?
There are several reasons why an @Autowired field in a Spring bean might be null:
The field has not been properly annotated with
@Autowired. Make sure that you are using the@Autowiredannotation on the field, and not on the setter method or constructor.The field is not in a Spring bean.
@Autowiredonly works on fields that are part of a Spring bean. If you are trying to@Autowirea field that is not in a Spring bean, it will not be injected.The bean that the field is in has not been properly registered with the Spring application context. Make sure that the bean is either annotated with
@Componentor@Service, or is listed in the@ComponentScanannotation.There is no matching bean in the Spring application context to inject. Make sure that there is a bean of the correct type in the application context, or consider using the
@Qualifierannotation to specify which bean to inject.The field is being accessed before it has been injected.
@Autowiredfields are injected by the Spring container after the bean has been constructed, so make sure that you are not accessing the field before it has been injected.
If you are still having trouble, try enabling debug logging for the Spring framework to get more information about what is happening during the injection process. You can do this by adding the following line to your log4j.properties file:
log4j.logger.org.springframework=DEBUG