Sobes.tech
Middle

What is the lifecycle of Spring Beans?

sobes.tech AI

Answer from AI

  1. Initialization:

    • Creating the Bean object.
    • Filling properties via Dependency Injection (@Autowired, @Value, etc.).
    • Executing the setBeanName() method (if the Bean implements BeanNameAware).
    • Executing the setBeanFactory() method (if the Bean implements BeanFactoryAware).
    • Executing the setApplicationContext() method (if the Bean implements ApplicationContextAware).
    • Executing the postProcessBeforeInitialization() methods of BeanPostProcessor.
    • Executing initialization methods:
      • Methods annotated with @PostConstruct.
      • The method specified in the initMethod attribute of the Bean definition.
      • The afterPropertiesSet() method (if the Bean implements InitializingBean).
    • Executing the postProcessAfterInitialization() methods of BeanPostProcessor.
  2. Usage:

    • The Bean is ready for use.
  3. Destruction (when the Spring context is closed):

    • Executing the postProcessBeforeDestruction() methods of DestructionAwareBeanPostProcessor.
    • Executing destruction methods:
      • Methods annotated with @PreDestroy.
      • The method specified in the destroyMethod attribute of the Bean definition.
      • The destroy() method (if the Bean implements DisposableBean).
    • Executing the postProcessAfterDestruction() methods of DestructionAwareBeanPostProcessor.
// 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
What is the lifecycle of Spring Beans? — Java - sobes.tech