How Does Spring Framework Implement the Dependency Injection Pattern?

Inversion of Control (IoC) is a design principle where control over the flow of execution is handed over to a framework. This separates the management and application code. When developing a module, this approach frees you from needing to know about other program modules and the details of their interactions. Such code becomes more reusable and modular, reducing coupling.

Dependency injection (DI) is one implementation of IoC. In interaction with other modules, the program operates using high-level abstractions, while the concrete implementation is provided by the framework.

The usual implementation of DI involves the framework instantiating all services and storing them in an IoC container. A special entity, the Service Locator, is responsible for finding matches between implementations and abstractions and injecting them.

Spring consists of a large set of various libraries. DI is implemented by one of the main libraries – Spring IoC.

Business logic entities in Spring, similar to JavaEE, are called beans. Beans are declared in various ways, most of which originate from the concept of Configuration. The container for beans is represented by ApplicationContext. To delegate the initialization of a dependency to the context, it is marked with the @Autowired annotation.