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
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
:
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.
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.
BeanFactory: Doesn't support event propagation.
ApplicationContext: Provides a built-in mechanism for event propagation, allowing beans to listen to and publish events.
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.
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.
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.
BeanFactory: Doesn't support internationalization.
ApplicationContext: Provides support for message bundles, allowing for internationalization (i18n) of applications.
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.
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
.
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.
Event handling and listeners in BeanFactory and ApplicationContext:
ApplicationContext
supports an event mechanism where beans can publish and listen to events. This feature is not available in BeanFactory
.// ApplicationContext example public class MyEventPublisher { @Autowired private ApplicationContext applicationContext; public void publishEvent(String message) { applicationContext.publishEvent(new MyCustomEvent(this, message)); } }