Buns of Aluminum
Buns of Aluminum

Reputation: 2439

Spring ApplicationContext Bean Scope

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?

Upvotes: 15

Views: 14632

Answers (2)

k-debugger
k-debugger

Reputation: 7

By default a bean created in Spring is of scope singleton. However, if you use Spring DispatcherServlet and DispatcherPortlet, a bean scope is requested.

Upvotes: -2

Jonathan Holloway
Jonathan Holloway

Reputation: 63672

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

Upvotes: 19

Related Questions