lishman levelup
«previous  next»


Getting Started
Form Processing
IoC Container
Hibernate ORM



Spring MVC Object Scopes


By default, the @Autowired annotation injects objects as singletons. That is, a single object for a type is instantiated by the Spring IoC Container, and re-used each time it is injected into an annotated field of that type. However, other scopes are also available.

Scopes

Info Singleton is by far the most common scope.
An object can be instantiated with one of these scopes:
  • singleton - a single object instance.
  • prototype - a new instance, each time the object is injected.
  • request - a new instance for each HTTP request.
  • session - an instance for the lifetime of an HTTP Session.

@Scope

The @Scope annotation specifies the scope to use for instances of the annotated class.
@Scope("prototype")
@Controller
public class CountryController {
  ..
}
Tip
Tip - remember to make singletons thread-safe.
»