What is Inversion of Control ?

Khushi Vashishtha
CodeX
Published in
4 min readJul 10, 2024

The primary purpose of the IOC (Inversion of Control) Container in Spring is to handle the creation and management of bean objects based on the configurations defined in the configuration file. This means that the control over Java objects and their lifecycle is managed by the Spring framework rather than by the developers themselves, hence the term “Inversion of Control.”

By offloading the responsibility of managing object creation and dependencies to the IOC Container, developers can focus on building the application logic without worrying about the intricacies of object instantiation and lifecycle management. This leads to more modular, testable, and maintainable code.

The main purposes of the IOC Containers in Spring applications are:

  1. Recognize and read data from the Spring configuration file.
  2. Identify Bean classes and their locations specified in the configuration file.
  3. Create, manage, and provide Bean objects to the Spring application.
  4. Assemble dependencies between objects.

Spring Framework provides the following two types of IOC Containers.

1. BeanFactory

2. ApplicationContext

Types of IOC Container

1. BeanFactory

  • Fundamental Container: BeanFactory is the base container in Spring, capable of performing essential functions such as identifying bean classes, creating their objects, and supplying these objects to Spring applications.
  • Reading Configuration: It reads all bean configuration details from the Spring configuration file into a Resource object.
  • On-Demand Creation: It retrieves bean configuration details from the Resource object and creates bean objects on demand as per application requirements.
  • Base for Other Containers: BeanFactory serves as the foundational container for all IOC containers, including ApplicationContext.
  • Outdated Container: BeanFactory is considered outdated and does not support advanced Spring features such as Event Navigation Model, Internationalization (I18N), AOP, and Web Application Development.
  • Interface and Implementation: Spring provides an interface called “BeanFactory” and an implementation class “XmlBeanFactory” to represent the BeanFactory container.

BeanFactory beanFactory = new XmlBeanFactory(resource);

BeanFactory

Note: XmlBeanfactory is deprecated in Spring3.x version

2. ApplicationContext IOC Container

ApplicationContext is an advanced version of the BeanFactory IOC Container, providing additional features such as Internationalization, Event Handling, and more, while retaining all fundamental functionalities of BeanFactory. In Spring, the ApplicationContext IOC Container is represented by a predefined interface, which is a child interface of BeanFactory.

Spring offers three main implementation classes for ApplicationContext:

  1. ClassPathXmlApplicationContext :
  • Retrieves bean configuration details from a configuration file located in the application classpath.
ClassPathXmlApplicationContext

2. FileSystemXmlApplicationContext :

  • Retrieves bean configuration details from a configuration file located on the system hard disk.

3. AnnotationConfigApplicationContext:

  • Configures the application context using annotated classes.
  • Accepts @Configuration classes and @Component classes.
AnnotationConfigApplicationContext ( AppConfig.java )
AnnotationConfigApplicationContext ( Test.java )

These implementations enhance the functionality of the ApplicationContext container, providing greater flexibility and advanced capabilities for Spring applications.

Differences Between BeanFactory and ApplicationContext IOC Containers

  1. Fundamental vs. Advanced Functionality :
  • BeanFactory: Fundamental IOC Container providing basic functionalities like creating and maintaining bean objects.
  • ApplicationContext: Extension of BeanFactory with advanced features like Internationalization and Event Handling, in addition to the fundamental functionalities.

2. AOP Services Integration:

  • BeanFactory: Does not support integrating AOP services such as Security and JTA.
  • ApplicationContext: Supports integrating AOP services, making it suitable for more complex applications.

3. Suitability for Web Applications:

  • BeanFactory: Not suitable for web applications based on the Spring web module.
  • ApplicationContext: Suitable for web applications and fully supports the Spring web module.

4. Instantiation Strategy:

  • BeanFactory: Uses Lazy Instantiation/Initialization, creating singleton objects when the getBean() method is first called.
  • ApplicationContext: Uses Early Instantiation/Initialization, creating singleton objects at the time of ApplicationContext startup.

5. Scope Support:

  • BeanFactory: Supports only Singleton and Prototype scopes.
  • ApplicationContext: Supports all Spring scopes, including Singleton, Prototype, Request, Session, GlobalSession, and WebSocket.

6. Application Type:

  • BeanFactory: Mainly used for standalone applications.
  • ApplicationContext: Suitable for all types of Spring framework applications.

7. Container Status:

  • BeanFactory: Considered outdated in modern Spring applications.
  • ApplicationContext: Actively used and not outdated, providing a robust feature set for contemporary Spring development.

--

--

Khushi Vashishtha
CodeX
Writer for

CS student | Java Developer | Tech Blogger @ Medium | Sharing Java insights, tutorials, and tips.