Bean scopes in Spring Framework

Bean scopes in Spring Framework


Spring Bean scopes

Set up the scope

  • The @Scope annotation can be used
@Scope("singleton")
  • There are 6 scopes are available in the application context

singleton

  • This is the default scope.
  • The same object is returned every time.
  • Better to use for all stateless beans.

prototype

  • A new object is created every time.
  • Better to use for all stateful beans.

request

  • Here it will create a single instance and it will available in the complete lifecycle of an HTTP request
  • Only valid in ApplicationContext

session

  • Here it will create a single instance and it will available in the complete lifecycle of an HTTP session
  • Only valid in ApplicationContext

application

  • Here it will create a single instance and it will available in the complete lifecycle of the servlet context
  • Only valid in ApplicationContext

websocket

  • Here it will create a single instance and it will available in the complete lifecycle of the web socket
  • Only valid in ApplicationContext

0 comments :

Post a Comment