# Spring Bean Life Cycle

[![](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/scaled-1680-/image-1650155826887.png)](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/image-1650155826887.png)


- When the class is created we can see there are 'Aware' interfaces that get inialized
	- BeanNameAware
	- BeanFactoryAware
	- ApplicationContextAware
* * *

### Shutdown

**Container Shutdown -> Disponable Bean's destroy() -> Call custom destroy method -> Terminated**

* * *
### Callback Interfaces
- Spring has two interfaces you can implement for call back events
- *InitializingBean.afterPropertiesSet()*
	- called after properties are set
- *DisposableBean.destroy()*
	- called during bean destruction in shutdown

### Life Cycle Annotations
- Spring has two annotations you can use to hook into the bean life cycle
- *@PostConstruct* 
	- called after the bean has been constructed but before its returned to the requesting object
- *@PreDestroy* 
	- called just before the bean is destroyed by the container

* * *
### Bean Post Processors
- Gives you a means to tap into the Spring context life cycle and interact with beans as they are processed
- Implement interface BeanPostProcessor
- *postProcessBeforeInitialization*
	- called before bean initialization method
- *postProcessAfterInitialization*
	- called after bean initialization

Note: The guru admits in all his years he has never used these

* * *
### 'Aware' Interfaces
- Spring has over 14 Aware Interfaces
- These are used to accses the Spring Framework infrastructure
- Rarely used by Devs, mostly used within the framework itself
[![](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/scaled-1680-/image-1650155856418.png)](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/image-1650155856418.png)
The more useful ones:
- ApplicationEventPublisherAware - used for creating custom events inside spring and set up event listeners
- BeanFactoryAware - If you need to handle a bean within a process



# Spring Bean Scopes
- Singleton (default) - Only one instance of the bean is created in the IoC container
- Prototype - A new instance is created each time the bean is requested
- Request - Single instance per http request.*
- Session - Single instance per http session.*
- Global-session - A single instance per global session. Typically only used in a Portlet context.*
- Application - bean is scoped to the lifecycle of a ServletContext.*
- Websocket - Scopes a single bean definition to the lifecycle of a WebSocket.*
- Custom scope - extensible, define your own "Scope" interface. See docs for details

*Only valid in the contex of a web-aware Spring ApplicationContext

[![](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/scaled-1680-/image-1650155883613.png)](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/image-1650155883613.png)
No declaration is needed for singleton scope.

[![](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/scaled-1680-/image-1650155896014.png)](https://bookstack.mitchellhenschel.com/uploads/images/gallery/2022-04/image-1650155896014.png)
In Java configuration use the @Scope annotation. We see the xml configuaration above.