Back to: Spring Framework Tutorials
Introduction to Spring Core Container
In this article, I am going to give you a brief introduction to Spring Core Container. Please read our previous article, where we discussed MVC in Spring Framework.
Introduction to IoC
IoC is a design principle. The IOC is utilized to achieve the loose coupling among the various application classes. IoC is responsible for inverting the flow of control as compared to the traditional control flow. The IoC and Dependency injection patterns are used to remove the dependencies from the code. IoC is a design pattern that is used to decouple the components and layers in an application. The IoC pattern is implemented by injecting dependencies into the components when it is constructed. The IoC is good as it allows us to test the components independently and the program complexity can be reduced.
IoC (inversion of Control) means to create the instances of dependencies first and then instances of a class (optionally injecting them through constructor), instead of creating an object of the class and then the class instance creating instances of dependencies.
Introduction to Spring Container / IOC Container
The spring core container is the Core Module of the Spring framework. The container is responsible for creating the objects, wiring the objects together, configuring the objects, and managing the complete life cycle from creation and destruction. The Spring container uses the Dependency Injection for managing the components that make up an Application.
The IoC container is responsible for managing the automatic dependency injections through the application. In order to achieve the loosely coupled classes, we have to use DIP, DI, and IoC Container. The Diagram below illustrates how to achieve the loosely coupled design in a step-by-step manner.
Types of Dependency Injections
Following are the types of Dependency Injection.
- Setter Injection
- Constructor Injection
- Aware Injection
- Method Injection
- Lookup Method Injection
The Setter injection is nothing but injecting the bean dependencies using the setter methods of an object. In this type of Injection, the object is first created and then the dependency is injected. In Setter injection, we use the property elements for injecting the dependencies. Given below code snippet is an example of setter injection.
The Constructor injection is a mechanism of DI in which the dependencies are injecting into the constructor of the class. The Constructor injection is a mechanism of injecting the dependencies by specifying them as parameters to the Class’s constructor. The Class that needs the dependency must expose a public constructor that takes an instance of the required Dependency as a constructor argument.
The aware injection is a mechanism of injecting a particular framework object to the bean through a callback style method. The object that is being injected depends on the interface which the bean implements.
The Method injection is a mechanism of DI in which the methods are injecting instead of the objects. The key in Method Injection is that a method can be overridden to return to another bean in the container.
Lookup Method Injection
The Lookup method injection is a mechanism of dynamically overriding a registered bean method. The method should be annotated with @Lookup. The method annotated with the @Lookup tells spring framework to return the instance of the methods return type when the method is invoked.
In the next article, I am going to give a brief introduction to Design Patterns. Here, in this article, I gave a brief introduction to Spring Container and I hope you enjoy this Spring Core Container article.