DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

spring-001:implementations-of-ApplicationContext

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.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more