The BeanFactory
is the most basic container in Spring, responsible for instantiating, configuring, and managing beans.
The Bean Lifecycle refers to the series of steps a bean goes through from creation to destruction within the Spring IoC (Inversion of Control) container.
✅ Complete Lifecycle Steps in BeanFactory
Bean Definition Loading
-
The container reads and parses the bean definitions (XML, annotations, or Java config).
-
-
Bean Instantiation
-
The Spring container creates an instance of the bean using a constructor or a factory method.
-
-
Populate Properties (Dependency Injection)
-
Spring performs dependency injection by setting properties or constructor arguments.
-
-
BeanNameAware
-
If the bean implements
BeanNameAware
, Spring callssetBeanName(String name)
passing the bean’s name.
-
-
BeanFactoryAware
-
If the bean implements
BeanFactoryAware
, Spring callssetBeanFactory(BeanFactory beanFactory)
passing the BeanFactory itself.
-
-
ApplicationContextAware
(if ApplicationContext used)-
If the bean implements
ApplicationContextAware
, Spring injects the context viasetApplicationContext()
.
-
-
BeanPostProcessor#postProcessBeforeInitialization()
-
All registered
BeanPostProcessor
s get a chance to modify the bean before initialization callbacks.
-
-
Initialization Callbacks
-
Spring executes one or more of the following:
-
@PostConstruct
annotated method -
afterPropertiesSet()
fromInitializingBean
interface -
Custom
init-method
specified in XML or Java config
-
-
-
BeanPostProcessor#postProcessAfterInitialization()
-
Spring again calls all
BeanPostProcessor
s after initialization callbacks.
-
-
Bean is Ready for Use
-
Now the bean is fully initialized and managed by the container.
-
-
Container Shutdown Begins
-
Destruction Callbacks
-
On container shutdown, Spring invokes:
-
@PreDestroy
annotated method -
destroy()
fromDisposableBean
interface -
Custom
destroy-method
from XML or Java config
-
-
📌 Diagram Summary
✅ Example (with key lifecycle hooks)
⚠️ Note:
-
BeanFactory
supports all lifecycle hooks shown, but most advanced features (like@PostConstruct
,@PreDestroy
,ApplicationContextAware
) are more commonly used withApplicationContext
, which is a superset of BeanFactory.
🌿 What is ApplicationContext
?
ApplicationContext
is a central interface to Spring’s advanced IoC container, building on top of BeanFactory
. It not only manages beans but also provides:
-
Internationalization support
-
Event publication
-
Bean post-processing
-
Resource loading
-
Integration with Spring AOP, Environment, etc.
🔁 Lifecycle of ApplicationContext
When Spring initializes and shuts down the application context, beans go through a standard lifecycle, as described below.
✅ Bean Lifecycle in ApplicationContext
-
Bean Definition Parsing
-
Spring parses configuration (
@Configuration
, XML, annotations like@Component
, etc.)
-
-
Bean Instantiation
-
Beans are created using constructors or factory methods.
-
-
Dependency Injection
-
Spring injects dependencies using
@Autowired
, constructors, setters, or fields.
-
-
Aware Interfaces Called
-
If a bean implements these interfaces, Spring calls:
-
BeanNameAware
:setBeanName()
-
BeanFactoryAware
:setBeanFactory()
-
ApplicationContextAware
:setApplicationContext()
-
EnvironmentAware
,ResourceLoaderAware
, etc.
-
-
-
BeanPostProcessors – Before Initialization
-
All registered
BeanPostProcessor
s havepostProcessBeforeInitialization()
called.
-
-
Custom Initialization
-
Spring executes initialization callbacks:
-
@PostConstruct
annotated method -
afterPropertiesSet()
fromInitializingBean
-
Custom
initMethod
defined in@Bean(initMethod="...")
or XML
-
-
-
BeanPostProcessors – After Initialization
-
Then Spring invokes
postProcessAfterInitialization()
for eachBeanPostProcessor
.
-
-
✅ Bean Ready
-
The bean is fully initialized and available for use.
-
-
Context Events (Optional)
-
ApplicationContext
can publish lifecycle events like:-
ContextRefreshedEvent
-
ContextStartedEvent
-
ContextStoppedEvent
-
ContextClosedEvent
-
-
-
Destruction Phase (on shutdown)
-
When
ApplicationContext
is closed (viacontext.close()
or application shutdown):-
@PreDestroy
is called -
destroy()
fromDisposableBean
-
Custom
destroyMethod
from@Bean(destroyMethod="...")
-
📌 Lifecycle Diagram
✅ Sample Code: Full Bean Lifecycle
💡 Key Differences from BeanFactory
Feature | BeanFactory | ApplicationContext |
---|---|---|
Basic DI container | ✅ | ✅ |
BeanPostProcessors | ⚠️ Manual | ✅ Automatic |
Event handling support | ❌ | ✅ |
Internationalization (i18n) | ❌ | ✅ |
Annotation scanning support | ❌ | ✅ |
Aware interfaces support | Partial | ✅ |
Resource loading | Basic | Rich |
ApplicationContext
when:-
You want full Spring features (events, environment, post-processors)
-
You're building real-world applications
-
You use
@Component
,@Configuration
, or annotation-driven development
No comments:
Post a Comment