Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring ApplicationContext Bean Scope

Tags:

java

spring

When you create a Service bean or Dao bean in your Spring applicationContext.xml file, what is the scope of those beans?

Will every person who accesses the web application use the same instance of the bean, or is the bean instantiated for each user's session?

like image 489
Buns of Aluminum Avatar asked Jul 30 '09 23:07

Buns of Aluminum


People also ask

What is the scope of a bean in Spring?

This scopes the bean definition to a single instance per Spring IoC container (default). This scopes a single bean definition to have any number of object instances. This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.

What is the scope of Bean in portlet context?

global-session is scope of bean in portlet context.

Which of the following are not a valid scope of Spring bean?

Explanation: In Spring 1. x, singleton and prototype are the only two valid bean scopes, and they are specified by the singleton attribute (i.e., singleton=”true” or singleton=”false”), not the scope attribute.


1 Answers

By default a bean created in Spring is of scope singleton, so yes each person will access the same instance in those cases. The alternative is to specify the scope as prototype.

More info on this here, sections 3.4.1 and 3.4.2:

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes-prototype

like image 118
Jon Avatar answered Oct 15 '22 15:10

Jon