Inversion of Control (IoC) is a design principle in the Spring Framework (and other frameworks) that shifts the responsibility for managing object lifecycles and dependencies from the application code to the framework. In Spring, IoC is achieved through the Dependency Injection (DI) mechanism.
Press enter or click to view image in full size
Ioc in Spring
How Inversion of Control Works in Spring
Container Responsibility: Spring provides an IoC container that is responsible for managing the lifecycle of beans (objects) and their dependencies. Beans are configured declaratively (via XML or annotations) or programmatically (via Java configuration).
Dependency Injection: Spring injects the required dependencies into beans either at runtime or during initialization, without the bean explicitly instantiating or locating its dependencies. This can be done using:
Constructor Injection: Dependencies are provided through the class constructor.
Setter Injection: Dependencies are set using setter methods.
Field Injection: Dependencies are injected directly into fields using annotations (e.g., @Autowired).