1. Overview
In this article, first we will introduce the concepts of Inversion of Control (IoC) and Dependency Injection (DI), then we'ill exaplain how the annotation @Autowired works in Spring Boot.
2. Inversion of Control
Inversion of Control (IoC) is a design principle used by framework libraries that allow the framework to regain some control from the application, that moves the responsibility of the object lifecycle (as the creation, dependecy settings...) from the application (as developer) to the framework. The difference between IoC and traditional programming is that instead of having our custom code call libraries, a framework can control the flow of a program and call our code directly.
3. Dependency Injection
Using dependency injection we can implement IoC, where we invert the control that comes with setting an object's dependencies. It is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself. It's a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.
Dependencies can be injected into objects in many ways for example, by using the constructor or setter injection techniques, in fact one can use specialized frameworks for dependency injection (for example, Spring) to accomplish that, but they are certainly not necessary. When we explicitly create and pass objects (dependencies), it is itself an dependency injection.
Example of direct construction:
public class ComplexAlgorithmImpl {
private BubbleSortAlgorithm bubbleSortAlgorithm = new BubbleSortAlgorithm();
//...
}
Example of Spring Boot IoC/DI:
public interface SortAlgorithm {
public int[] sort(int[] numbers);
}
@Service
public class ComplexAlgorithmImpl {
@Autowired
private SortAlgorithm sortAlgorithm;
//..
}
3. Spring Boot IoC Container
In Spring, the beans and application-context are the basis for Spring Framework's IoC container. The objects that make up the backbone of your application are called beans, a bean is instantiated, assembled, and managed by a Spring IoC container. Beans are stored in the application-context and every application has an access to it.
When a bean is created, through the annotiation @Autowired, the framework searches in the application-context for an instance that matches to the requested object, when the framework find the instance, the reference is passed back and injected in the client. If a matching instance does not already exist in the application-context the framework will create new one.
The deafault beans are of type Signleton, which means that only one instance can be created per class, so if we have several classes that depen on the same object, they will all have the same reference to the object in the application-context. You can specify the bean type prototype to create a new instance of any dependency declaration of the same class, the other types are: request, session, global session, and application. For more read here
4. Conclusion
This article has explained the Inversion of Control, Dependency Injection, and how Spring Boot manages them.