Spring's IoC container manages the lifecycle of beans by creating, configuring, and assembling them during application startup, and by managing their states and dependencies through the entire runtime.
The Big Picture
Think of the IoC container as a sophisticated manager in a large corporation. This manager is responsible for hiring employees (creating beans), assigning them tasks (configuring beans), and ensuring they have everything they need to perform their tasks effectively (managing dependencies). The IoC container does this for your application components (beans), handling their lifecycle from creation to destruction.
Core Concepts
- Bean Definition: A bean definition contains the information needed to create and configure a bean.
- Bean Scope: The scope determines the lifecycle and visibility of a bean.
- Singleton: A single instance per Spring IoC container.
- Prototype: A new instance every time a bean is requested.
- Other scopes like
request
,session
, etc., for web applications.
- Bean Lifecycle Callbacks: Methods that can be called at specific points in a bean's lifecycle.
- Initialization: Methods called after a bean is created and its dependencies are set.
- Destruction: Methods called before a bean is destroyed.
Detailed Walkthrough
Bean Creation and Initialization
When the Spring IoC container starts, it performs the following steps for each bean:
- Instantiate: The container creates an instance of the bean.
- Populate Properties: The container injects the dependencies into the bean's properties.
- Set Bean Name: If the bean implements
BeanNameAware
, the container passes the bean's name. - Set Bean Factory: If the bean implements
BeanFactoryAware
, the container passes a reference to itself. - Pre-Initialization: The container calls
BeanPostProcessor
'spostProcessBeforeInitialization
methods. - Initialize Bean: The container calls custom initialization methods specified via
@PostConstruct
,InitializingBean
'safterPropertiesSet()
, or a custom init-method. - Post-Initialization: The container calls
BeanPostProcessor
'spostProcessAfterInitialization
methods.
Bean Destruction
When the Spring IoC container shuts down or a singleton bean is removed, it performs the following steps:
- Pre-Destruction: The container calls custom destruction methods specified via
@PreDestroy
,DisposableBean
'sdestroy()
, or a custom destroy-method.
Example
Here's an example demonstrating these steps using annotations and Java-based configuration.
Java Configuration:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(initMethod = "customInit", destroyMethod = "customDestroy")
public MyBean myBean() {
return new MyBean();
}
}
MyBean Class:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class MyBean {
public MyBean() {
System.out.println("Bean instantiated");
}
@PostConstruct
public void init() {
System.out.println("PostConstruct method called");
}
public void customInit() {
System.out.println("Custom init-method called");
}
@PreDestroy
public void destroy() {
System.out.println("PreDestroy method called");
}
public void customDestroy() {
System.out.println("Custom destroy-method called");
}
}
Main Class:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
context.close(); // This triggers the destruction lifecycle
}
}
Understanding Through an Example
In this example:
- The
MyBean
instance is created whenmyBean()
method is called. @PostConstruct
method is called after the bean is fully initialized.- Custom
init-method
is called after@PostConstruct
. - When the context is closed,
@PreDestroy
method is called. - Custom
destroy-method
is called after@PreDestroy
.
Conclusion and Summary
Spring's IoC container manages the lifecycle of beans by handling their creation, initialization, and destruction. It provides hooks for customization through lifecycle callbacks and bean scopes, allowing fine-grained control over how beans are managed. This results in a more flexible and maintainable application structure.
Test Your Understanding
- Describe the difference between Singleton and Prototype scopes in Spring.
- What methods can be used to define custom initialization and destruction logic for a bean?
- Create a bean with a custom initialization method and demonstrate its lifecycle in a Spring application.
Reference
- Spring Framework Documentation on Bean Lifecycle: Bean Lifecycle
Would you like to explore any specific lifecycle aspect or have any questions on the examples provided?
'300===Dev Framework > Spring' 카테고리의 다른 글
Spring IoC Container Introduced (0) | 2024.06.02 |
---|---|
Spring Bean Introduced (1) | 2024.06.02 |
Hibernate Settings Advanced (0) | 2024.06.01 |
Spring Security Introduced (0) | 2024.05.29 |
How do you specify the base packages for component scanning in a Spring configuration class? (0) | 2024.05.29 |