Spring – IOC Container

The most important component of the Spring Framework is the Spring Core Container which creates the objects, wires them together, manages the configuration and overall object life cycle from creation of the object till its destruction. We can also say that The IoC container is responsible to instantiate, configure and assemble the objects. The Spring container uses Dependency Injection (DI) to manage the components that make up an application. The below diagram depicts that the application classes are combined with the configuration metadata which creates and initializes the ApplicationContext and provides a fully customized application, ready for use

Spring IOC Container

Spring Containers

Spring provides two types of containers.

  • Spring BeanFactory container
  • Spring ApplicationContext container

Spring Bean Factory Container

Spring Bean Factory Container uses the org. springframework. beans. factory. BeanFactory interface and provides the support for Dependency Injection (DI) by registering different beans and their dependencies. The XmlBeanFactory is the implementation class for the BeanFactory interface. The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

Resource resource=new ClassPathResource(“applicationContext.xml”);  

BeanFactory factory=new XmlBeanFactory(resource); 

Spring ApplicationContext Container

The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. Use the below given syntax to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as given below:

ApplicationContext context =   new ClassPathXmlApplicationContext(“applicationContext.xml”);  

spring- applicationContext container

The constructor of ClassPathXmlApplicationContext class receives string, so we can pass the name of the xml file to create the instance of ApplicationContext.

Below given are the possible ways for implementing the Inversion of Control (Spring IoC Container):

  1. With the usage of Factory Pattern
  2. With the usage of Service Locator Pattern
  3. With the Inclusion of Dependency Injection through
  • The Constructor Injection
  • The Setter Injection
  • The Interface Injection