How is Dependency injection applied to Spring Boot (part 2)?

Tram Ho

1. Inversion of Control (IoC)

1.1. What is IoC?

In the previous section we learned about Dependency inversion principles, and learned the principles for better coding. However, the DI principle is simply the principle, and its implementation is unclear. We can do the same as before, creating each object separately, then attaching them together (via constructor).

However, if the program has many modules and many objects, the situation will be as follows:

  • Missing module into another module
  • Pay attention to the order in which the module is initialized.
  • Ring dependent (A depends on B, and B also depends on A, so we don’t know whether to create A or B first)

The way to create individual modules, then reattach is called the normal way. Due to the above disadvantages, nowadays people give the concept of IoC – Inversion of Control – reversing control.

IoC aims to simplify the process of object creation and the association between them, by following the principle: Don’t create objects, just describe how they will be created.

Because the process is complicated and difficult to implement, there are many frameworks that come out that support IoC, typically Spring for Java or Angular of JavaScript.

IoC framework will have available components to create and manage objects in the program. IoC will manage, analyze dependencies, create objects in the most suitable order and link them together, according to the programmer description.

Inversion of control has many types of implementation, such as using ServiceLocator, Delegate, … but the most popular is Dependency injection. I will talk about it in the next section.

1.1. Code examples

As above, thanks to our IoC should not themselves create by new modules as before. We do not need to write the following code to create two modules goodEngine and myCar .

The above code we both create and link the two objects together. But not with the IoC framework, we just need to mark (mark) on the classes. IoC framework will rely on that to create the correct module as required.

Each class marked with @Component (this is called Annotation in java) will be understood by IoC as a module:

  • @Component is to tell the IoC container to create a single object (singleton).
  • @Autowired is to find the corresponding module (created from before) and inject it into it.

Thanks to the IoC framework, it’s much easier to create and link modules. However, IoC should only be applied on modules, large objects, and small, miscellaneous objects that are used temporarily should not be used.

2. Dependency injection

2.1. What is Dependency injection?

DI is an implementation of IoC, by injecting the module into another module that needs it. For example, high-level module Car needs Engine module, then dependency injection will follow the steps as follows:

  • Find and create modules corresponding to Engine (class that has Engine interface implements)
  • Create a Car module, because Car depends on Engine , IoC finds and retrieves the Engine object created earlier and injects it into the Car .
  • Accomplished

Every module in IoC is called dependency, although there are modules that are not dependent on any other module. When the program runs, IoC will scan all classes marked with dependencies, create a single object (singleton), and put it in a bag called the IoC container, and use it whenever needed. Therefore, the modules ensure IoC create only one object, saving memory and managing it easier.

If when creating a module, but that module needs another module, IoC will look in the IoC container to see if there is, if so, inject, if not, create a new one, put it in the container and inject. The automatic injecting of dependencies (modules) like this is called Dependency injection .

Note small, need to distinguish DI (dependency injection) with the DI principle (dependency inversion principle) offline.

2.2. Types of injection

There are 2 main types:

  • Constructor-based injection: Use injected required modules. The injected modules are in the constructor, and are assigned in turn to the fields.
  • Setter-based injection: Use injected optional modules. Each module will be injected through the setter, located in parameters and also assigned to certain fields.

Take a look at the following code to understand what constructor based and setter based look like.

3. Apply to Spring Boot

Spring is a framework built on the principle of Dependency injection. Spring itself contains an IoC container, which is responsible for creating and managing modules:

  • Spring’s IoC container is called Application context
  • The modules contained in the IoC container are called Beans

Spring Boot uses annotations like @Component to mark up the class, indicating that the class needs to create a module. In addition to @Component , there are other annotations such as @Repository , @Controlller , @Service , … also marked as module.

When the Spring Boot application runs, the IoC container will perform the following process:

  • Scan classes marked Bean, and create a singleton object, put in the IoC container
  • When there is a Bean dependent on another Bean, the IoC will look in the container, if not, create it, if there is, then get it and inject it into the bean that needs it.

Basically, in the next section we will discuss more closely about two concepts Bean and Application context offline. See you in the next articles. If you find a good and useful article, don’t forget to upvote to motivate yourself.

Share the news now

Source : Viblo