Sunday 10 July 2016

Spring/IoC container

The Spring/IoC container is at the core of the Spring Framework. It is responsible to instantiate, wire them together, configure them, and manage their complete lifecycle from creation till destruction.

The spring container uses dependency injection (DI) to manage the components that make up an application. The IoC container gets informations from the XML file and works accordingly.

Major tasks performed by IoC container:
To instantiate the application class
To configure the object
To assemble the dependencies between the objects

Different type of IoC containers:
1. BeanFactory
interface org.springframework.beans.factory.BeanFactory
The XmlBeanFactory is the implementation class for the BeanFactory interface.
Resource resource = new ClassPathResource("applicationContext.xml");  
BeanFactory factory = new XmlBeanFactory(resource); 
Employee s = (Employee) factory.getBean("e");
The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

2. ApplicationContext
interface org.springframework.context.ApplicationContext
The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface.
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...