How does Dependency Inversion differ from Dependency Injection? Does every bean need an interface?
Short answer
DIP is a principle: both business code and infrastructure depend on an abstraction, and the abstraction belongs to the business side. DI is a technique: an object receives its dependencies from outside (constructor) instead of calling new itself.
3 points to say:
- DIP = principle, DI = technique, IoC container = tool; DI helps implement DIP but is not the same thing.
- The "inversion" is that the interface belongs to the business side; injecting
MySqlOrderDaodirectly is DI without DIP. - Constructor injection,
finalfields; interfaces for ports and places with several implementations, not for every bean.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.