如何在Spring中使用View Scope

时间:2015-12-06 08:59:14

标签: java spring jsf view-scope

逗人,

我尝试在Spring中使用View Scope,我可以直截了当地完成以下步骤:

1-创建视图范围如下:

    package scope;

    import java.util.Map;
    import javax.faces.context.FacesContext;
    import org.springframework.beans.factory.ObjectFactory;
    import org.springframework.beans.factory.config.Scope;

    public class ViewScope implements Scope {

       @Override
        public Object get(String name, ObjectFactory<?> objectFactory) {
            Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

            if(viewMap.containsKey(name)) {
                return viewMap.get(name);
            } else {
                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) {
            //Not supported
        }

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

2-在配置类中,将bean注册为以下代码:

   @Configuration
    ..............
  public class Application implements Serializable{
    ...............
    ...............

     @Bean
     public CustomScopeConfigurer viewScope () {
         CustomScopeConfigurer configurer = new CustomScopeConfigurer ();
         configurer.addScope("view", new ViewScope());
         return configurer;
     }

3-在Spring组件类中,使用名称为“view”的注释范围:

    @scope(“view”)

谢谢&amp;此致

0 个答案:

没有答案