-1

I have a basic question. I've been reading through some tutorials about spring and hibernate integration and in most of them there are DAO and Service layers like below:

  public interface TeamDAO {

  public void addTeam(Team team);
   public void updateTeam(Team team);
  public Team getTeam(int id);
   public void deleteTeam(int id);
  public List<Team> getTeams();

 }

Then the implementation of the DAO is provided using the SessionFactory. For example:

 @Repository
 public class TeamDAOImpl implements TeamDAO {
        @Autowired
        private SessionFactory sessionFactory;

       //Implementation follows..
 }

And then there's another service interface the same as the DAO interface like below:

public interface TeamService {

 public void addTeam(Team team);
public void updateTeam(Team team);
public Team getTeam(int id);
public void deleteTeam(int id);
public List<Team> getTeams();

 }

And the service implementation:

 @Service
 @Transactional
 public class TeamServiceImpl implements TeamService {
   //HERE IS MY QUESTION
   @Autowired
   private TeamDAO teamDAO;

   //implementation follows
}

In the service implementation above where I marked "here is my question" I see that we inject only the interface TeamDAO which doesn't have the implementation of the TeamDAOImpl class. So how does the interface and its implementations get injected together in the service layer provided we only inject the interface TeamDAO and not TeamDAOImpl?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
george
  • 3,102
  • 5
  • 34
  • 51
  • 1
    Your question is covered in any decent tutorial of Spring. Spring will do the dependency injection, it will choose the implementation for the interface and set the field with an object reference. – Luiggi Mendoza Mar 01 '15 at 21:22
  • can you post a link to such a decent tutorial, please? Why will spring choose the implementation of the interface? – george Mar 01 '15 at 21:44

2 Answers2

0

Spring injects TeamDAOImpl because it gets register as spring bean when you mark it as @Repository

Farm
  • 3,356
  • 2
  • 31
  • 32
  • Yes, but you don't say @Autowired TeamDAOImpl but TeamDAO which is the interface. How does that inject the implementation as well? – george Mar 01 '15 at 21:40
  • In OO there is concept called "Program to an interface". For More check : http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface Spring will choose implementation of the interface and sets the object reference. The reason tutorial uses interface handle (TeamDAO ) instead of actual object reference is because that way you can have different implementation of your interface. Useful for mocking / unit testing. – Farm Mar 01 '15 at 21:45
  • Also, it's worth noting that Spring can inject a proxy of the implementation rather than the direct implementation in order to enable AOP functionability e.g. transaction management per method name. – Luiggi Mendoza Mar 01 '15 at 21:55
0

When you use @Autowired on an interface, Spring searches a bean instance whose class implements that interface. If it doesn't find any such bean, it fails. If it finds more than one class that implement the interface, it fails. Please refer to Spring @Autowired documentation for further details.

fps
  • 33,623
  • 8
  • 55
  • 110