Managed beans with custom ViewScope

2019-06-04 06:18发布

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.

2条回答
三岁会撩人
2楼-- · 2019-06-04 06:24

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?

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-04 06:44

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.

查看更多
登录 后发表回答