Spring Bean Scopes and Lifecycle

Last Updated on: August 1, 2021 pm

Spring Bean Scopes and Lifecycle

Bean Scopes

1
2
3
4
<bean id="myCoach"
class="com.luv2code.spring.demo.TrackCoach"
scope="prototype">
</bean>

Singleton - Default

  • The Spring Container only creates only one instance of the bean.
  • It’s cached in memory.
  • All requests for the bean - will return a shared reference to the SAME bean.

Prototype

Creates a new bean instance for each container request.

Bean Lifecycle

Bean Lifecycle

Custom methods

  1. Defining init and destroy methods

  2. Configure the method names in Spring config file

Reference: Udemy, Spring & Hibernate for Beginners (including SpringBoot)