Inversion of control
Also called: IoC
A design where a framework creates your objects and calls your code, instead of your code creating everything and calling the framework.
Definition
With inversion of control, your classes declare what they need and a container (Spring's ApplicationContext, Hilt, a composition root you write yourself) builds the object graph and decides when your code runs. Dependency injection is the most common form. It keeps classes focused on their job and makes them easy to test with fakes.
In practice
You never call new OrderService(...) in a Spring app; Spring creates it and passes it to the controller that needs it.
Learn it in context
Lessons that use this term.
Related terms
- Dependency injectionGiving an object the things it needs from outside, instead of letting it create them itself.
- Spring beanAn object that Spring creates, configures and hands to other objects, usually one shared instance per application.
- Composition rootThe one place at the top of an app where the real dependencies are created and connected.