title: JSF View scope in Spring
body: <p>Is there any scope like JSF <code>@ViewScoped</code> in Spring 3.0? I have an application using JSF+Spring where backing beans are managed by Spring. I didn't find any scope like JSF wiew scope in Spring. I saw the blog <a href="http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0" rel="nofollow noreferrer">Porting JSF 2.0’s ViewScope to Spring 3.0</a>, but it didn't work for me.</p>
  
  <p>Here's my attempt on the custom Spring scope:</p>
  
  <pre><code>import java.util.Map;
  
  import javax.faces.context.FacesContext;
  
  import org.springframework.beans.factory.ObjectFactory;
  import org.springframework.beans.factory.config.Scope;
  
  /**
   * Implements the JSF View Scope for use by Spring. This class is registered as a Spring bean with the CustomScopeConfigurer.
  */
  public class ViewScope implements Scope {
  
      public Object get(String name, ObjectFactory&lt;?&gt; objectFactory) {
  
          System.out.println("**************************************************");
          System.out.println("-------------------- Getting objects For View Scope ----------");
          System.out.println("**************************************************");
          if (FacesContext.getCurrentInstance().getViewRoot() != null) {
              Map&lt;String, Object&gt; viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
              if (viewMap.containsKey(name)) {
                  return viewMap.get(name);
              } else {
                  Object object = objectFactory.getObject();
                  viewMap.put(name, object);
                  return object;
              }
          } else {
              return null;
          }
      }
  
      public Object remove(String name) {
          System.out.println("**************************************************");
          System.out.println("-------------------- View Scope object Removed ----------");
          System.out.println("**************************************************");
  
          if (FacesContext.getCurrentInstance().getViewRoot() != null) {
              return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
          } else {
              return null;
          }
      }
  
      public void registerDestructionCallback(String name, Runnable callback) {
          // Do nothing
      }
  
      public Object resolveContextualObject(String key) {         return null;
      }
  
      public String getConversationId() {
          return null;
      }
  
  }
  </code></pre>
  
  <p><code>application-context.xml</code>:</p>
  
  <pre><code>&lt;bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"&gt;
      &lt;property name="scopes"&gt;
              &lt;map&gt;
                  &lt;entry key="view"&gt;
                      &lt;bean class="com.delta.beans.ViewScope"/&gt;
                  &lt;/entry&gt;
              &lt;/map&gt;
          &lt;/property&gt;
   &lt;/bean&gt;
  </code></pre>