Explain the Spring Bean Scopes

One possible question for a Java Interview could be: what can you tell me about the spring bean scopes?

In Spring Framework we have 5 (five) bean scopes:

  • SINGLETON which means that only one instance of a bean is allowed per container (in other words, per application);
  • PROTOTYPE which means that when you request the from the container, a new instance of that bean is created every time when the bean is requested;
  • REQUEST means Spring allows a single bean instance per HTTP request;
  • SESSION, a single bean instance is allowed per HTTP session;
  • GLOBAL-SESSION which means a single bean instance is allowed per global HTTP session.

Further, to anticipate the next question, you should prepare the answer to what autowired is. And which are the type of annotation for defining a bean:

  • @Component
  • @Controller – used for presentation layer
  • @Service – used for service layer (aka, used to create a service to execute a business logic)
  • @Repository – used for DAO layer (aka, used to create a repository to access the database).
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply