11/06/23 What are IoC, DI and the Spring IoC Container?

2023. 6. 11. 19:25카테고리 없음

There're some important concepts in Spring Framework. The following contents are from https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

1. Inversion of Control

Inversion of Control is a principle in software engineering which transfers the control of objects or portions of a program to a container or framework.

 

In contrast with traditional programming, in which our custom code makes calls to a library, IoC enables a framework to take control of the flow of a program and make calls to our custom code. To enable this, frameworks use abstractions with additional behavior built in. If we want to add our own behavior, we need to extend the classes of the framework or plugin our own classes.

 

The advantages of this architecture are:

  • decoupling the execution of a task from its implementation
  • making it easier to switch between different implementations
  • greater modularity of a program
  • greater ease in testing a program by isolating a component or mocking its dependencies, and allowing components to communicate through contracts

We can achieve Inversion of Control through various mechanisms such as: Strategy design pattern, Service Locator pattern, Factory pattern, and Dependency Injection(DI).

2. Dependency Injection(DI)

Dependency injection is a pattern we can use to implement IoC, where the control being inverted is setting an object's dependencies. Connecting objects with other objects, or “injecting” objects into other objects, is done by an assembler rather than by the objects themselves.

3. The Spring IoC Container

An IoC container is a common characteristic of frameworks that implement IoC. In the Spring framework, the interface ApplicationContext represents the IoC container. The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.

#

 

I recommend reading Part III. Core Technologies from the Spring Framework Reference Documentation. Here is the link : https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/spring-core.html