spring-001:implementations-of-ApplicationContext

Hunor Vadasz-Perhat - Jan 28 - - Dev Community

Spring provides several implementations of the ApplicationContext interface, tailored for different use cases and application types. Here's a breakdown of the key implementations:


1. Annotation-Based Configuration

  • AnnotationConfigApplicationContext
    • A standalone implementation for working with Java-based configuration (@Configuration, @Bean, @Component, etc.).
    • Ideal for modern Spring applications where XML configuration is not used.
    • Commonly used in Spring Boot or standalone applications.

2. XML-Based Configuration

  • ClassPathXmlApplicationContext

    • Loads bean definitions from an XML configuration file located in the classpath.
    • Common in older Spring applications.
  • FileSystemXmlApplicationContext

    • Similar to ClassPathXmlApplicationContext, but the XML configuration file is loaded from a file system path instead of the classpath.

3. Web-Specific Contexts

  • XmlWebApplicationContext

    • A web-specific implementation that loads XML bean definitions.
    • Typically used in traditional Spring MVC applications.
    • Integrates with web.xml configurations or servlet context.
  • AnnotationConfigWebApplicationContext

    • A web-specific implementation for Java-based configuration.
    • Used in modern Spring MVC or Spring Boot applications for web environments.
    • Allows configuring web-related beans via annotations like @Controller, @RestController, etc.

4. Generic Application Contexts

  • GenericApplicationContext

    • A flexible implementation that provides a general-purpose application context.
    • Can register beans dynamically via registerBean() or registerSingleton() methods.
    • Often used as a base class for custom implementations.
  • GenericWebApplicationContext

    • A web-specific variant of GenericApplicationContext.
    • Can be programmatically configured in web applications.

5. Reactive Context

  • ReactiveWebApplicationContext
    • Designed for reactive applications built with Spring WebFlux.
    • Supports non-blocking, event-driven architecture.

6. Special-Purpose Contexts

  • StaticApplicationContext

    • A simple, programmatic implementation for testing or quick prototypes.
    • Allows you to register beans programmatically without XML or annotations.
    • Rarely used in real applications.
  • ConfigurableApplicationContext

    • Not a standalone implementation but an extended interface for application contexts that are configurable (e.g., refresh(), close()).
    • Implemented by other contexts like AnnotationConfigApplicationContext.

Commonly Used Implementations

Implementation Use Case
AnnotationConfigApplicationContext Java-based configuration for standalone apps.
ClassPathXmlApplicationContext XML configuration loaded from the classpath.
AnnotationConfigWebApplicationContext Java-based configuration for web apps.
XmlWebApplicationContext XML configuration for web apps.
GenericApplicationContext Programmatic, general-purpose context.

How Many in Total?

Counting variations and less commonly used implementations, Spring provides around 8-10 distinct implementations of ApplicationContext. However, in practice, only a few of these (like AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext) are used extensively in modern applications.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .