本文介绍了使用自定义ViewScope的托管bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Hibernate Provider的Spring 3.1.0.RELEASE,JSF 2.x,JPA 2做一个Web应用程序.我使用PrettyFaces 3.3.2作为友好的URL.该应用程序在Tomcat 6.35上运行.

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 .

我想使用Jsf ViewScope,所以我决定遵循在Web上找到的实现: http://comdynamics.net/blog/109/spring3-jsf2-view-scope/

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;
    }
}

我注意到@PreDestroy并没有像显示此问题一样被调用 @PreDestroy从未调用过在@ViewScoped 上.

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

这是否意味着带有ViewScope的托管bean永不被破坏?哪些行为会导致内存泄漏.我们应该使用这个范围吗?

这仅在Spring或Mojarra上使用自定义Viewscope发生吗?

谢谢.

推荐答案

问题是视图范围的错误实现.它创建了Spring bean objectFactory.getObject();,但是从不销毁它.

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

要解决它-检查正确的实现,并支持registerDestructionCallback.

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

BWT,当前的Mojjara实现不会在您的bean too 上调用@PreDestory .但是,它将至少释放bean实例.

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

这篇关于使用自定义ViewScope的托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 22:09
查看更多