Spring Dependency Injection / Inversion of Control

Dependency Injection is sometimes interchangeable with the Inversion of Control (IoC). When a standalone program starts, it starts the main program, creates the dependencies, and then proceeds to execute the appropriate methods. However, this is exactly the reverse if IoC is applied. That is, all the dependencies and relationships are created by the IoC container and then they are injected into the main program as properties. The program is then ready for action. This is essentially the reverse of usual program creation and hence is called Inversion of Control principle. The DI and IoC are often used interchangeably.

Spring allows us to inject the properties via constructors or setters. Figure 3.1 illustrates Dependency injection involves giving an object its dependencies as opposed to an object having to acquire those dependencies on its own.

Figure 3.1 Figure 3.1

Related Links

Constructor Type Injection

Setter Type Injection

No comments yet.

Leave a Reply