Managed beans with custom ViewScope

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 06:22:12

问题


I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider. I use PrettyFaces 3.3.2 for friendly URL. The application run on Tomcat 6.35 .

I wanted to use the Jsf ViewScope so I decided to follow the implementation found on the web : http://comdynamics.net/blog/109/spring3-jsf2-view-scope/

public class ViewScope implements Scope {

    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();

        Object instance = viewMap.get(name);
        if (instance == null) {
            instance = objectFactory.getObject();
            viewMap.put(name, instance);
        }
        return instance;
    }

    @Override
    public Object remove(String name) {
        logger.debug("ViewScope::remove {}", 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;
    }
}

I notice that @PreDestroy are not called on them like show this question @PreDestroy never called on @ViewScoped.

Does it mean that the Managed beans with ViewScope are never destruct ? Which conduct to memory leak. Should we use this scope so?

It's only happen with custom Viewscope on Spring or also on Mojarra ?

Thanks.


回答1:


Problem is incorrect implementaiton of view scope. It is creates Spring bean objectFactory.getObject(); but never destroy it.

To solve it - check correct implementation with support for registerDestructionCallback.

BWT, current Mojjara implementation will not call @PreDestory on your bean too. But it will free bean instance at least.




回答2:


I tried the work around for Jsf view scope bean memory leaks using spring custom view scope. It works for both Jsf 2.1 & 2.2.Try the code in below link. Memory leak with ViewScoped bean?



来源:https://stackoverflow.com/questions/10888084/managed-beans-with-custom-viewscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!