Posts

Showing posts with the label lifecycle

Spring Bean Life Cycle

 In Spring, the life cycle of a bean refers to the various stages that a bean goes through from its instantiation to its destruction. Spring provides several callback methods and interfaces that allows to interact with the bean during these stages. The typical life cycle of a Spring bean includes the following steps: 1. Instantiation : The bean is created by the Spring container through its defined instantiation mechanism. This can be done using either constructor injection or setter injection. 2. Populate Properties : After the bean is instantiated, Spring injects any necessary dependencies (properties or constructor arguments) into the bean. 3. BeanNameAware : If the bean implements the `BeanNameAware` interface, the Spring container sets the bean's name through the `setBeanName()` method. 4. BeanFactoryAware or ApplicationContextAware : If the bean implements the `BeanFactoryAware` or `ApplicationContextAware` interface, the Spring container sets the reference to the bean factor...