The Artima Developer Community
Sponsored Link

Java Community News
Custom Bean Scopes with Spring

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Custom Bean Scopes with Spring Posted: Jan 4, 2007 12:07 PM
Reply to this message Reply
Summary
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.
Advertisement

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.

private static ThreadLocal conversation = new ThreadLocal();
  
public void doFilter(
    ServletRequest request, 
    ServletResponse response,
    FilterChain chain) 
    throws IOException, ServletException {
    
        String conversationId = null;
        if (request instanceof HttpServletRequest) {
            conversationId = 
                ((HttpServletRequest) request).getRequestURI();
        }
        conversation.set(conversationId);
       try {
          chain.doFilter(request, response);
       } finally {
          conversation.remove();
       }
}   

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:


private static final ObjectFactory MAP_FACTORY = 
    new ObjectFactory() {
        public Object getObject() {
            return new HashMap();
        }
    };

public String getConversationId() {
    return PageScopeFilter.getConversationId();
}

public synchronized Object get(
    String name, 
    ObjectFactory objectFactory) {
    Map beanMap = 
       (Map) get(scope, getConversationId(), MAP_FACTORY);
    return get(beanMap, name, objectFactory);
}

public synchronized Object remove(String name) {
    Map beanMap = (Map) scope.get(getConversationId());
    return beanMap==null ? null : beanMap.remove(name);
}
  
private Object get(Map map, 
                   String name, 
                   ObjectFactory factory) {
    Object bean = map.get(name);
    if(bean==null) {
        bean = factory.getObject();
        map.put(name, bean);
    }
    return bean;
}

What do you think of the way Spring 2.0 implements custom scopes?

Topic: What's On Your Java SE 7 Wish List? Previous Topic   Next Topic Topic: Jakarta Project Releases Commons Virtual File System 1.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use