I am working on a web application using Java and Spring. I'm new at Spring, so as an opportunity to learn, I was basically given an existing application and was told to extend it. I am having trouble understanding how the @Autowired work. I understand the high-level concept of dependency injection, but I want to know how the @Autowired annotation knows which concrete implementation of an interface to inject?
To put my question into context, I will explain the problem I am having:
I have an interface called PostDao, and a class called PostDaoImpl which implements PostDao. I then have another class called PostDaoPublicImpl which extends PostDaoImpl. These classes exist in my persistence layer.
I then have an interface called PostService, and a class called called PostServiceImpl which implement PostService. I then have another class called PostServicePublicImpl which extends PostServiceImpl. These classes exist in my services layer.
In PostServiceImpl the following line injects the following object:
@Autowired private PostDao postDao;
//This injects an object of the class PostDaoImpl
My problem is, in PostServicePublicImpl how do I have the same declaration as above, but have it inject an object of the class PostDaoPublicImpl:
@Autowired private PostDao postDao;
//This injects an object of the class PostDaoPublicImpl
I feel as though if I understand how the @Autowired annotation works, then I will be able to solve this problem. Any help and suggestions to solve the problem would be greatly appreciated.