Simple JSF View Scope for Spring

Light version of:

https://github.com/michail-nikolaev/primefaces-spring-scopes
https://github.com/javaplugs/spring-jsf

without destruction callback listener support

and

http://forum.spring.io/forum/spring-projects/web/72898-view-scope-with-spring

ideas


package org.nkey.primefaces.scopes.test.spring.scope;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PreDestroyViewMapEvent;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/**
 * @author m.nikolaev Date: 21.11.12 Time: 0:37
 */
public class ViewScope implements Scope, Serializable{
    private static final Logger LOGGER = LoggerFactory.getLogger(ViewScope.class);
    
    @Override
    public Object get(String name, ObjectFactory objectFactory) {
        final Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

		if (viewMap.containsKey(name)) {
			Object bean = viewMap.get(name);			

			// // restore a transient autowired beans after re-serialization bean
			//WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
			//AutowireCapableBeanFactory autowireFactory = webAppContext.getAutowireCapableBeanFactory();
			
			//if (webAppContext.containsBean(name)) {
				// Reconfigure resored bean instance.
				//bean = autowireFactory.configureBean(bean, name);
			//}
			// // end restore
			
			return bean;
		} else {
			final Object object = objectFactory.getObject();
			viewMap.put(name, object);

			return object;
		}
    }

    @Override
    public Object remove(String name) {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }

    @Override
    public String getConversationId() {
        return null;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {
        
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }


package org.nkey.primefaces.scopes.test.spring.scope;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author m.nikolaev Date: 21.11.12 Time: 1:01
 */
@Qualifier
@Scope("view")
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SpringViewScoped {
}

applicationContext.xml

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="org.nkey.primefaces.scopes.test.spring.scope.ViewScope"/>
                </entry>
            </map>
        </property>
    </bean>