Simple JSF View Scope for Spring

Please use:

Light version of:

without destruction callback listener support



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.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);
    public Object get(String name, ObjectFactory objectFactory) {
        final Map 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;

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

    public String getConversationId() {
        return null;

    public void registerDestructionCallback(String name, Runnable callback) {

    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
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface SpringViewScoped {



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s