A interesting new feature in Spring 2.0 is the ability to define custom scopes for bean lifecycles. In a recent blog post, Eugene Kuleshov shows how to define a custom scope tied to the lifetime of a page.
A new feature in Spring 2.0 is custom scopes: the ability to specify custom life-cycle for Spring beans. In an interview with Artima late last year, Spring project founder Rod Johnson noted that:
Spring has historically provided two scopes for beans. There's a singleton scope—an object whose life-time is tied to that of the owning Spring container. When the container comes across a bean definition, it holds a strong reference [to] it for the duration of the runtime...
The other scope is the prototype scope. Every time you ask the container for an instance of a component with the name of a bean with prototype scope, you get a distinct, but identically configured, instance... With the prototype ... scope, every time you inject a component with a given name, you're injecting an independent, but identically configured instance.
In Spring 2.0, we add the notion of custom scope. Unlike the the singleton and prototype scopes, you can have a scope with an arbitrary name, and back that [scope] using an out-of-the-box implementation that Spring provides, or using a custom implementation. We have out-of-the-box implementations for HTTP session and HTTP servlet request scopes...
Custom scopes is an extensible, general mechanism, and is not tied to the traditional Web application middle-tier. It can be opened up to allow a wider range of out-of-the-box possibilities, and also a greater range of custom ones, too.
Custom scopes can include beans scoped for the lifetime of a session, or even for the lifetime of a page. The latter is demonstrated in a recent blog post by Eugene Kuleshov, More fun with Spring scopes:
In [the case of page scope], conversation is bound to the page in web application (or even to each unique set of request parameters for that page). Practical examples include per-page caching of the static data (i.e. for Ajax use), allow page visitors to interact or edit page content together and many others...
In the blog post, Kuleshov shows how to configure Spring for page-scoped beans. The first step is to capture the identity of the conversation in order to identify what page scope to associate with a request. While Kuleshov notes that in practice many URLs may be mapped to the same page scope, to keep his example simple, he assumes that each URL represents a unique scope. To map URL requests to a scope,
I can use a servlet filter, which can be registered in web.xml and run before Spring's own DispatcherServlet. This filter will store requestURI in thread local and provide static accessor method for retrieving that value.
Having identified the conversation, Kuleshov defines a custom page scope, an implementation of Spring's Scope interface. An interesting aspect of this code is how an ObjectFactory is used to obtain references to beans for the custom scope: