I'm having trouble getting @Autowired to work in a class annotated by @Service, the autowired variable always is null. Let me explain:
@Service
public class Searcher extends Thread implements ISearcher {
@Autowired
protected ISessionProvider sessionProvider; <-- always null
...
public Searcher() {
sessionProvider.doSomeStuff();
}
sessionProvider here is always null.
The strange thing is that the same autowire in a @Controller does work:
@Controller
@RequestMapping("/search")
@Secured({ "ROLE_USER" })
public class SearchController extends BaseController {
@Autowired
protected ISessionProvider sessionProvider; <-- does work
@Autowired
protected ISearcher searcher;
The last line throws exception because the constructor of Searcher (implementing ISearcher) tries to access sessionProvider, which is null.
I am not sure what i might be doing wrong, it looks like spring doesn't autowire the ISessionProvider in Searcher.
It might be that spring first autowires the Searcher in SearchController, but it should first autowire SessionProvider in Searcher and next autowire Searcher in SearchController. Cause searcher cannot be autowired without a valid SessionProvider. Puzzles my brain ;-)
Can somebody offer a helping brain?
[edit]
- component-scan includes my services, controllers and everything, just checked.
- the I before interfaces is indeed not very nice (old habits)
- not sure if this is a "duplicate" question, mainly because i'm not doing anything with "new", i let do spring do all the hard work, but i'll take a better peek.