Spring IoC Container

One of the most important topic when we start learning Spring is Spring IoC container. We need to understand what a Spring IoC Container is, and how does it work, to understand how Spring works. We’ll understand this by answering few important questions about Spring IoC container.

What is Spring IoC Container?

Spring IoC container is responsible for instantiating, configuring and assembling the beans. It is the Spring IoC container which does the core of the job. Spring IoC container is responsible of implementing Dependency Injection and Inversion of Control (IoC) principles on which Spring’s popularity is based.

Spring IoC Container is responsible for managing beans. Beans is just an object used in application but in Spring’s context when managed by container is called a bean.

How is Spring IoC container represented in Spring?

Spring IoC container is represented by org.springframework.context.ApplicationContext interface. ApplicationContext is a sub-interface of BeanFactory interface.

This is read-only while the application is running. This interface provides:

  • Bean factory methods for accessing application components.
  • The ability to publish events to registered listeners.
  • Message resource handling used in internationalization.
  • Application specific contexts such as the WebApplicationContext for use in web applications.

BeanFactory provides the basic functionality whereas the ApplicationContext provides many more functionalities used in enterprise applications. Following are the known implementing classes of ApplicationContext:


How does Spring IoC container work?

The instructions can be provided to the container in either of these formats:

  • XML
  • Java annotations
  • Java configuration code

The container does the following in short:

Container reads the business objects and convert into beans to be used in the execution of application by applying the configuration metadata.

A Java application is usually made up objects communicating with each other. An object may have a dependency on another object. These objects may not work alone. We define the dependencies in XML, Java annotations or Java configuration code. The container reads these instructions and produces the beans which then can be used in applications. This is the core function of Spring IoC container.