Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring - Difference Between BeanFactory and ApplicationContext

BeanFactory and ApplicationContext are both core interfaces in the Spring framework for configuring and retrieving beans. They both serve as a container for beans but offer different levels of functionality and features. Here are the key differences between BeanFactory and ApplicationContext:

1. Basic vs. Advanced:

  • BeanFactory: It's the root interface for accessing the Spring container and provides basic DI (Dependency Injection). It's a low-level mechanism to access beans.

  • ApplicationContext: Extends the BeanFactory interface and adds more enterprise-centric features. It's a higher-level mechanism and is more commonly used in modern Spring applications.

2. Configuration Metadata:

  • BeanFactory: Typically reads the configuration metadata from XML files using XmlBeanFactory.

  • ApplicationContext: Can read from a variety of sources, not just XML. Supports configuration annotations and Java-based configuration.

3. Event Publishing:

  • BeanFactory: Doesn't support event propagation.

  • ApplicationContext: Provides a built-in mechanism for event propagation, allowing beans to listen to and publish events.

4. Built-in Enterprise Features:

  • BeanFactory: Has a smaller feature set and lacks built-in support for aspects, transactions, etc.

  • ApplicationContext: Comes with built-in support for aspects, messaging, transactions, and other enterprise features.

5. Resource Access:

  • BeanFactory: Provides basic support for accessing resources.

  • ApplicationContext: Offers advanced capabilities to access resources, such as retrieving resources from a web URL, classpath, or the file system.

6. AOP Integration:

  • BeanFactory: Has no built-in support for AOP, though you can still manually integrate AOP if needed.

  • ApplicationContext: Provides integrated AOP support, allowing for declarative aspect-oriented programming with beans.

7. Internationalization:

  • BeanFactory: Doesn't support internationalization.

  • ApplicationContext: Provides support for message bundles, allowing for internationalization (i18n) of applications.

8. Eager vs. Lazy Loading:

  • BeanFactory: Uses lazy loading by default. Beans are instantiated when called by getBean() method.

  • ApplicationContext: Initializes all singleton beans upon context startup, leading to eager loading by default. This ensures that all configuration issues are discovered immediately.

9. Web Application Context:

  • BeanFactory: Doesn't have a specialized context for web applications.

  • ApplicationContext: Has a variant called WebApplicationContext, specifically for web applications. This allows for interaction with web-specific contexts like ServletContext.

Recommendation:

In modern Spring applications, it's usually recommended to use ApplicationContext because of its richer feature set. The BeanFactory is more suited for lightweight applications or scenarios where memory consumption is a concern.

  1. Event handling and listeners in BeanFactory and ApplicationContext:

    • Description: ApplicationContext supports an event mechanism where beans can publish and listen to events. This feature is not available in BeanFactory.
    • Code:
      // ApplicationContext example
      public class MyEventPublisher {
          @Autowired
          private ApplicationContext applicationContext;
      
          public void publishEvent(String message) {
              applicationContext.publishEvent(new MyCustomEvent(this, message));
          }
      }