Previous | Home | Next |
The Spring container (Inversion of Control) is at the core of the Spring Framework. The container will create the objects, 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. These objects are called Spring Beans.
The Spring container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code. The main tasks performed by IOC container are as follows-
- Instantiate the application class
- Configure the object
- Assemble the dependencies between the objects

Generally there are two types of IOC containers. That are as given below
Spring BeanFactory: This is the simplest container providing basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.
In BeanFactory
The XmlBeanFactory is the implementation class for the BeanFactory interface. To use the BeanFactory, we need to create the instance of XmlBeanFactory class as given below:
Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(resource);
Spring ApplicationContext: This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface. The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory.
In ApplicationContext
The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. We need to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as given below:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Following are two approaches used to provide the Implementation of IOC.
- Dependency Injection
- Dependency Lookup
Dependency Injection (DI): In case of DI (Dependency Injection) ,dependency of object are satisfied by IOC container on its own i.e IOC container are not ask to satisfied the dependency of an object.
Dependency Lookup (DL): In case of DL (Dependency Lookup) ,dependency of an object are satisfied only when IOC container is asked to do so.
Note: The spring framework does not support dependency lookup (DL) only support dependency injection (DI) approach.
Previous | Home | Next |