Spring - ApplicationContext

Last Updated : 29 Apr, 2026

ApplicationContext is an advanced interface in the Spring Framework that acts as a powerful IoC container for managing beans and their lifecycle. It extends BeanFactory and provides additional enterprise-level features like event handling, internationalization, and annotation-based configuration. It is widely used in modern Spring applications for efficient and scalable development.

  • Supports advanced features like event publishing, i18n, and AOP integration.
  • Provides eager bean initialization for better runtime performance.
  • Enables easy integration with annotation-based configuration like @Component and @Autowired.

Note: Due to its advanced features, developers generally prefer using ApplicationContext over BeanFactory. BeanFactory provides only basic functionalities and is suitable for lightweight applications or memory-constrained environments.

ApplicationContext Classes Hierarchy

Hierarchy of Spring ApplicationContext implementations, showing different configuration approaches for standalone and web-based applications.

applicationcontext_interface_
classes

1. AnnotationConfigApplicationContext

Spring container that manages beans using Java-based configuration with classes annotated like @Configuration and @Component, eliminating the need for XML. Supports multiple configuration classes and is commonly used in standalone and Spring Boot applications.

  • Uses classes annotated with @Configuration, @Component, or JSR-330 annotations
  • Supports multiple config classes (later beans can override earlier ones)
  • Eliminates XML configuration; widely used in standalone and Spring Boot apps
  • Bean overriding can be enabled via spring.main.allow-bean-definition-overriding=true

Syntax:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class, AppConfig1.class);

2. AnnotationConfigWebApplicationContext

Spring container for web applications that manages beans using annotation-based configuration and integrates with servlet components like DispatcherServlet. it initializes the web application context using classes annotated with @Configuration and @Component.

  • Web-specific version of AnnotationConfigApplicationContext
  • Works with DispatcherServlet and ContextLoaderListener
  • Supports register() and scan() for configuration
  • Can be configured using Java (WebApplicationInitializer) instead of web.xml

Syntax:

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class); // Register config class
context.setServletContext(container); // Set servlet context

Example 

Java
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class MyWebApplicationInitializer
    implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container)
        throws ServletException
    {

        AnnotationConfigWebApplicationContext context
            = new AnnotationConfigWebApplicationContext();

        context.register(AppConfig.class);
        context.setServletContext(container);

        // Additional servlet configuration can be done here
    }
}


Explanation: This class replaces web.xml and initializes the Spring web application at startup. It loads bean configuration from the XML file and links it with the servlet container.

3. XmlWebApplicationContext

Spring container for web applications that manages beans using XML configuration files and integrates with servlet components like DispatcherServlet. It initializes the web context using XML files such as applicationContext.xml.

  • Uses XML-based configuration for defining beans
  • Works with DispatcherServlet in Spring MVC
  • Can be configured via web.xml or WebApplicationInitializer
  • Suitable for traditional Spring MVC (XML-based setup)

Syntax:

XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocation("/WEB-INF/spring/applicationContext.xml");
context.setServletContext(container);
context.refresh();

Example:

Java
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class MyXmlWebApplicationInitializer
    implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container)
        throws ServletException
    {

        XmlWebApplicationContext context
            = new XmlWebApplicationContext();

        context.setConfigLocation(
            "/WEB-INF/spring/applicationContext.xml");
        context.setServletContext(container);

        // Additional servlet configuration
    }
}

Explanation: Creates and configures the XML-based Spring context for a web application during startup.

4. FileSystemXmlApplicationContext

Spring container that loads bean configuration from XML files using a file system path or URL instead of the classpath. It is mainly used in standalone applications and testing environments.

  • Loads XML configuration from a file system location
  • Suitable for standalone Java applications and testing
  • Allows direct access to beans via ApplicationContext

Syntax:

String path = "path/to/applicationContext.xml";
ApplicationContext context = new FileSystemXmlApplicationContext(path);

Example:

String path = "Documents/demoProject/src/main/resources/applicationcontext/student-bean-config.xml";
ApplicationContext context = new FileSystemXmlApplicationContext(path);
StudentService studentService = context.getBean("studentService", StudentService.class);

Explanation: Initializes the container from the file system and retrieves the StudentService bean.

5. ClassPathXmlApplicationContext

Spring container that loads bean configuration from XML files located in the classpath (e.g., src/main/resources). It is commonly used in standalone and testing environments for easy configuration.

  • Loads XML configuration from the classpath
  • Avoids using absolute file system paths
  • Suitable for standalone applications and testing

Syntax:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/student-bean-config.xml");

Example:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/student-bean-config.xml");
StudentService studentService = context.getBean("studentService", StudentService.class);

Implementation to use ApplicationContextContainer

Follow the below steps to implement and use the ApplicationContext container in a Spring application by using AnnotationConfigApplicationContext class.

Step 1: Create Spring Boot Project

Creating a Spring Project using Spring Initializer as pictorially depicted below.

  • Go to Spring Initializer and generate a project with required dependencies (Spring Web / Core)
  • Download the generated project (ZIP file)
  • Extract the ZIP file
  • Open the project in your IDE (IntelliJ / Eclipse)
out

Step 2: Create Student Class (POJO)

This class represents a simple bean (POJO) that will be managed by the Spring container

Java
public class Student {

    private int id;
    private String name;

    public Student() {}

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{id=" + id + ", name='" + name + "'}";
    }
}

Step 3: Create AppConfig Class (Configuration)

This class is used to define beans using Java-based configuration.

Java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public Student student() {
        return new Student(1, "Geek");
    }
}

Step 4: Create Main Class (Container Creation)

This step shows how container is created manually.

Java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {

    public static void main(String[] args) {

        ApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);

        Student student = context.getBean(Student.class);

        System.out.println(student);
    }
}

Note: This example uses manual container creation (AnnotationConfigApplicationContext) to understand how Spring works internally. In real applications, Spring Boot creates it automatically using SpringApplication.run().

Comment