Middle
What is the lifecycle of Spring Beans?
sobes.tech AI
Answer from AI
-
Initialization:
- Creating the Bean object.
- Filling properties via Dependency Injection (
@Autowired,@Value, etc.). - Executing the
setBeanName()method (if the Bean implementsBeanNameAware). - Executing the
setBeanFactory()method (if the Bean implementsBeanFactoryAware). - Executing the
setApplicationContext()method (if the Bean implementsApplicationContextAware). - Executing the
postProcessBeforeInitialization()methods ofBeanPostProcessor. - Executing initialization methods:
- Methods annotated with
@PostConstruct. - The method specified in the
initMethodattribute of the Bean definition. - The
afterPropertiesSet()method (if the Bean implementsInitializingBean).
- Methods annotated with
- Executing the
postProcessAfterInitialization()methods ofBeanPostProcessor.
-
Usage:
- The Bean is ready for use.
-
Destruction (when the Spring context is closed):
- Executing the
postProcessBeforeDestruction()methods ofDestructionAwareBeanPostProcessor. - Executing destruction methods:
- Methods annotated with
@PreDestroy. - The method specified in the
destroyMethodattribute of the Bean definition. - The
destroy()method (if the Bean implementsDisposableBean).
- Methods annotated with
- Executing the
postProcessAfterDestruction()methods ofDestructionAwareBeanPostProcessor.
- Executing the
// Example of using lifecycle annotations
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class MySpringBean {
private String myProperty;
public void setMyProperty(String myProperty) {
this.myProperty = myProperty;
System.out.println("4. setMyProperty() called: " + myProperty); // Example of property initialization step
}
@PostConstruct
public void customInitMethod() {
System.out.println("9. @PostConstruct method called."); // Example of initialization step - @PostConstruct
}
public void anotherInitMethodFromConfig() {
System.out.println("10. Custom initMethod from configuration called."); // Example of initialization step - initMethod
}
public void useBean() {
System.out.println("12. Bean is in use. Property: " + myProperty); // Example of usage step
}
@PreDestroy
public void customDestroyMethod() {
System.out.println("14. @PreDestroy method called."); // Example of destruction step - @PreDestroy
}
public void anotherDestroyMethodFromConfig() {
System.out.println("15. Custom destroyMethod from configuration called."); // Example of destruction step - destroyMethod in configuration
}
}
| Step | Description | Example |
|---|---|---|
| 1 | Creating Bean instance | new MySpringBean() |
| 2 | Filling properties (DI) | bean.setMyProperty("value") |
| 3 | BeanNameAware.setBeanName() |
Called if the Bean implements BeanNameAware |
| 4 | BeanFactoryAware.setBeanFactory() |
Called if the Bean implements BeanFactoryAware |
| 5 | ApplicationContextAware.setApplicationContext() |
Called if the Bean implements ApplicationContextAware |
| 6 | BeanPostProcessor.postProcessBeforeInitialization() |
Processing before initialization |
| 7 | InitializingBean.afterPropertiesSet() |
Called if the Bean implements InitializingBean |
| 8 | @PostConstruct method |
Method annotated with @PostConstruct |
| 9 | init-method from configuration |
Method specified in the initMethod attribute |
| 10 | BeanPostProcessor.postProcessAfterInitialization() |
Processing after initialization |
| 11 | Bean is ready for use | Calling Bean methods |
| 12 | DestructionAwareBeanPostProcessor.postProcessBeforeDestruction() |
Processing before destruction |
| 13 | DisposableBean.destroy() |
Called if the Bean implements DisposableBean |
| 14 | @PreDestroy method |
Method annotated with @PreDestroy |
| 15 | destroy-method from configuration |
Method specified in the destroyMethod attribute |
| 16 | DestructionAwareBeanPostProcessor.postProcessAfterDestruction() |
Processing after destruction |