In this article, you will learn the differences between IoC Container, Application Context and the Bean Factory in Spring Framework. The Spring IoC container is the core of the Spring Framework. It creates the class instances, wires them together, configures them, and manages their complete life cycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application.

If you are new to Spring Framework, checkout the related articles:

1. What is IoC Container in Spring?

In the article, Inversion of Control and Dependency Injection in Spring I have given you a details explanations of this. In the Spring framework, we don’t create the instance of the dependencies rather the framework does this for us.

IoC – Inversion of Control means, we don’t have the control to create the dependencies rather the framework does it for us. The code that is responsible for doing the dependency injection is known as IoC Container.

public class StudentService {

  private StudentRepository repository;

  // Other logics
public class StudentRepository {
    //persistence logic

In the above example, we don’t create the instance of StudentRepository manually, rather the spring creates it for us. Technically the Spring IoC Container creates it for us.

Inversion of Control is a technique that ensures decoupling, by transferring the objection creation job to a Container or Framework. The framework also allows us to customize the Objection creation and add custom behavior as per the developer’s needs. It is called inversion because the process is inverse where the bean creation and dependency is controlled by the container instead of the bean itself.

Advantages of IoC

  • Decoupling the objects and its executions.
  • Helps in runtime Polymorphism.
  • Better code Maintainability.
  • Easy code Testing due to loose coupling between the interfaces and implementations.

Two types of Spring IoC Containers

Spring provides the following two types of containers:

  • BeanFactory container
  • ApplicationContext container

2. What is BeanFactory in Spring?

The BeanFactory is also known as BeanFactory Container in Spring. It is the root interface to access any Spring bean container.

Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The BeanFactory is a central registry of application beans, their configurations, and dependencies.

The BeanFactory enables us to read bean definitions and access them using the bean factory as shown below. As you have already learned about the dependency injection before, I will only show you how to use BeanFactory as the base interface.

public class TestAutowireExample {
  public static void main(String[] args) {

    BeanFactory beanFactory = new AnnotationConfigApplicationContext(AutowireBeanConfig.class);


The full set of initialisation methods and their standard order for a managed bean is:

  • BeanNameAware’s setBeanName
  • BeanClassLoaderAware’s setBeanClassLoader
  • BeanFactoryAware’s setBeanFactory
  • EnvironmentAware’s setEnvironment
  • EmbeddedValueResolverAware’s setEmbeddedValueResolver
  • ResourceLoaderAware’s setResourceLoader (only applicable when running in an application context)
  • ApplicationEventPublisherAware’s setApplicationEventPublisher (only applicable when running in an application context)
  • MessageSourceAware’s setMessageSource (only applicable when running in an application context)
  • ApplicationContextAware’s setApplicationContext (only applicable when running in an application context)
  • ServletContextAware’s setServletContext (only applicable when running in a web application context)
  • postProcessBeforeInitialization methods of BeanPostProcessors
  • InitializingBean’s afterPropertiesSet
  • a custom init-method definition
  • postProcessAfterInitialization methods of BeanPostProcessors

On shutdown of a bean factory, the following lifecycle methods apply:

  • postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
  • DisposableBean’s destroy
  • a custom destroy-method definition

3. What is ApplicationContext in Spring

The ApplicationContext is a central interface to provide configuration for an application. This is read-only while the application is running but may be reloaded if the implementation supports this.

public class TestAutowireExample {
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(AutowireBeanConfig.class);


An ApplicationContext provides:

  • Bean factory methods for accessing application components. Inherited from ListableBeanFactory.
  • The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
  • The ability to publish events to registered listeners. Inherited from the ApplicationEventPublisher interface.
  • The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
  • Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

I recommend using the ApplicationContext wherever needed instead of the BeanFactory, as some of its implementations classes have been deprecated in Spring 5.x.


I have given you a details explanation of IoC containers i.e. ApplicationContext and BeanFactory. BeanFactory works first doing the heavy lifting jobs and then the ApplicationContext adds extra features like message source reading, message translations and etc.