Describe the Lifecycle of a Spring Bean

Beans are the central concern of the Spring Framework. Behind the scenes, the framework performs numerous processes with them. Many of these processes can be intercepted, allowing custom logic to be added at various stages of the bean's lifecycle. Each bean undergoes the following stages:

1. Instantiation: The technical beginning of the bean's life, the execution of its class constructor;

2. Properties population: Setting properties from the bean configuration and dependency injection;

3. Notification of aware interfaces: BeanNameAware, BeanFactoryAware, and others. These interfaces are described in another post. Technically, this is carried out by system subtypes of BeanPostProcessor and coincides with step 4;

4. Pre-initialization: The postProcessBeforeInitialization() method of the BeanPostProcessor interface;

5. Initialization: Various methods are applied in this order:
Bean method annotated with @PostConstruct from JSR-250 standard (recommended approach);
afterPropertiesSet() method for beans implementing the InitializingBean interface;
Custom init-method, defined per-bean in the initMethod parameter of its definition. In XML configuration, it can be set for all beans at once using default-init-method;

6. Post-initialization: the postProcessAfterInitialization() method of the BeanPostProcessor interface.
Describe the Lifecycle of a Spring Bean
When the IoC container completes its work, we can customize the stage of the bean's standard destruction. As with all Java finalization methods, there is no guarantee this stage will be invoked during a hard shutdown (kill -9). Three alternative "de-initialization" methods are called in the same order as their initialization counterparts:

1. Method annotated with @PreDestroy;
2. Method having the name from the destroyMethod bean definition property (or in the global default-destroy-method);
3.destroy() method of the DisposableBean interface.

It is important not to confuse the lifecycle of an individual bean with the lifecycle of the context and the stages of bean factory preparation. We will discuss these in future publications.