IOC Container

The core of the Spring Framework is its Inversion of Control (IoC) Container. Container is the factory of all java objects used in your applications.The IoC container manages life-cycle of java objects from instantiation to destruction. The container creates objects, wires them together, configures them, and manages their complete lifecycle from creation till destruction.

There are two key responsibilities of container:

  1. Manage life-cycle of bean

  2. Dependency injection of beans

Java components instantiated by IoC container are called beans (Java beans). Container uses dependency injection (DI) to wire or assemble the beans that build your Application.

Spring IoC is provides loose-coupling and dynamic binding between objects dependencies. To achieve loose coupling and dynamic binding Spring IOC container injects object dependencies and gives you the ready to use object.

Container also manages bean scope, lifecycle events, and AOP features for which bean has been configured and developed.

Spring IoC container classes are part of org.springframework.beans and org.springframework.context packages. BeanFactory is the root interface of Spring IoC container. ApplicationContext is the child interface of BeanFactory interface that provide Spring AOP features, i18n etc.

There are two types of IOC containers:

[1] Spring BeanFactory Container

This is the simplest container providing basic support for Dependency Injection (DI) and defined by the org.springframework.beans.factory.BeanFactory interface. It is light weight and mostly used in standalone spring applications. BeanFactory can still be used for light weight applications like mobile devices or applet based applications where data volume and speed is significant.

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

User dto = (User) factory.getBean("user");

See: TestBeanFactory.java

[2] Spring ApplicationContext Container

It is child of BeanFactory interface and adds more functionalities for enterprise applications such as i18n, AOP, event publishing, web context etc. This container is defined by the org.springframework.context.ApplicationContext interface. It is recommended over the BeanFactory Enterprise applications and web applications use application context container.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

User dto = (User) context.getBean("user");

See: TestXMLConf.java

ApplicationContext has number of useful implementation classes that we can use to get spring context and then beans.

  • ClassPathXmlApplicationContext: It is used in standalone applications where beans are configured using XML configuration. If you have bean configuration xml file in a standalone application, then you can use this.

  • FileSystemXmlApplicationContext: This is similar to ClassPathXmlApplicationContext except that the xml configuration file can be loaded from anywhere in the file system.

  • AnnotationConfigWebApplicationContext and XmlWebApplicationContext are used in web applications.

  • XmlWebApplicationContext is used in Spring MVC web application.

FAQ:

  1. What are the key responsibilities of the IOC container?

  2. What are the differences between BeanFactory and ApplicationContext containers?

    1. BeanFactory is a lightweight container.

  3. What is the relation between BeanFactory and ApplicationContext interface?

  4. Why is BeanFactory called a lightweight container?

  5. In your application which container have you used?

  6. Which are the different implementation classes of ApplicationContext containers?

  7. What are the different scopes of a bean?

  8. What are the differences between singleton and prototype scopes?