0

I am reading the Spring Hibernate CRUD tutorial from this url

http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/

Please can anyone tell me why in ContactController.java, ContactService interface is autowired instead of the class ContactServiceImpl.

Similarly in ContactServiceImpl ContactDAO interface is injected. Shouldn't we supposed to inject class instead of an interface?

Avi
  • 21,182
  • 26
  • 82
  • 121
Komal Sharma
  • 69
  • 1
  • 9

2 Answers2

4

When your code is dependent on interface and its implementation is injected by Spring, your code becomes decoupled with the implementation. This has an advantage that now you can swap in a different implementation without having to change the code that makes use of interface.

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
2

Spring is smart. It will find the implementation of the interface and inject it appropriately (or a proxy thereof.)

You should be programming to interfaces, not implementations.

Community
  • 1
  • 1
digitaljoel
  • 26,265
  • 15
  • 89
  • 115