Reputation: 69
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?
Upvotes: 0
Views: 1103
Reputation: 18772
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.
Upvotes: 4
Reputation: 26584
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.
Upvotes: 2