在 JBoss 7.1.1 上的 JavaEE6 项目( EJB3 , JSF2 )中,似乎我们有SeamFaces @ViewScoped
发生内存泄漏。
我们制作了一个原型来检查事实:
在测试结束时,我们使用VisualVM检查内存的内容,以及我们得到的内容:
@ViewScoped
bean,我们仍然可以获得有状态MyController
的200个实例 - 并且永远不会调用@PreDestroy
方法; @ConversationScoped
bean,@preDestroy
方法称为会话结束,然后我们得到了一个干净的内存。我们是否严重使用视图范围,还是真的是一个错误?
这是XHTML页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:s="http://jboss.org/seam/faces">
<f:metadata>
<f:viewParam name="u" value="#{myBean.uselessParam}" />
<s:viewAction action="#{myBean.callService}" />
</f:metadata>
<h:body >
<f:view>
</f:view>
</h:body>
</html>
现在包含的bean myBean
。对于@ConversationScoped
变体,所有注释的部分都是未注释的。
@ViewScoped
// @ConversationScoped
@Named
public class MyBean implements Serializable
{
@Inject
MyController myController;
//@Inject
//Conversation conversation;
private String uselessParam;
public void callService()
{
//if(conversation.isTransient())
//{
// conversation.begin();
//}
myController.call();
}
public String getUselessParam()
{
return uselessParam;
}
public void setUselessParam(String uselessParam)
{
this.uselessParam = uselessParam;
}
}
然后注入有状态bean MyController
:
@Stateful
@LocalBean
public class MyController
{
public void call()
{
System.out.println("call ");
}
@PreDestroy
public void destroy()
{
System.out.println("Destroy");
}
}
答案 0 :(得分:5)
我看到许多开发人员对Myface CODI中的@ViewAccessScoped感到满意。 你能试一试并告诉反馈。
答案 1 :(得分:5)
我在JSF托管的@ViewScoped bean中遇到了上述问题。在参考了几篇博客之后,我了解到JSF在http会话中保存了视图bean状态,并且仅在会话失效时才被销毁。每当我们在每次创建页面中引用的新视图范围bean时单击jsf页面。我做了一个使用Spring Custom View Scope的工作。它工作正常。以下是详细代码。
对于JSF 2.1:
步骤1:创建视图范围Bean Post构造监听器,如下所示。
public class ViewScopeBeanConstructListener implements ViewMapListener {
@SuppressWarnings("unchecked")
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
if (event instanceof PostConstructViewMapEvent) {
PostConstructViewMapEvent viewMapEvent = (PostConstructViewMapEvent) event;
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent();
List<Map<String, Object>> activeViews = (List<Map<String, Object>>)
FacesContext.getCurrentInstance().getExternalContext().getSessionMap(). get("com.org.jsf.activeViewMaps");
if (activeViews == null) {
activeViews = new ArrayList<Map<String, Object>>();
activeViews.add(viewRoot.getViewMap());
FacesContext.getCurrentInstance().getExternalContext().getSessionMap(). put("com.org.jsf.activeViewMaps", activeViews);
} else {
activeViews.add(viewRoot.getViewMap());
}
}
}
步骤2:在faces-config.xml中注册事件监听器
<system-event-listener>
<system-event-listener-class>
com.org.framework.custom.scope.ViewScopeBeanConstructListener
</system-event-listener-class>
<system-event-class>javax.faces.event.PostConstructViewMapEvent</system-event-class>
<source-class>javax.faces.component.UIViewRoot</source-class>
</system-event-listener>
步骤3:按如下方式创建自定义视图范围bean。
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 {
List<Map<String, Object>> activeViewMaps = (List<Map<String, Object>>)
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("com.org.jsf.activeViewMaps");
if (activeViewMaps != null && !activeViewMaps.isEmpty()
&& activeViewMaps.size() > 1) {
Iterator iterator = activeViewMaps.iterator();
if (iterator.hasNext()) {
Map<String, Object> oldViewMap = (Map<String, Object>)
iterator.next();
oldViewMap.clear();
iterator.remove();
}
}
Object object = objectFactory.getObject();
viewMap.put(name, object);
return object;
}
}
注意:其他重写方法可以为空。
对于JSF 2.2:
JSF 2.2将导航的视图映射保存在'com.Sun.faces.application.view.activeViewMaps'中的http会话中作为键。因此,在Spring Custom View Scope中添加以下代码。不需要JSF 2.1中的监听器
public class ViewScope implements Scope {
public Object get(String name, ObjectFactory objectFactory) {
Map<String, Object> viewMap =
FacesContext.getCurrentInstance().getViewRoot().getViewMap();
if (viewMap.containsKey(name)) {
return viewMap.get(name);
} else {
LRUMap lruMap = (LRUMap) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("com.sun.faces.application.view.activeViewMaps");
if (lruMap != null && !lruMap.isEmpty() && lruMap.size() > 1) {
Iterator itr = lruMap.entrySet().iterator();
while (itr.hasNext()) {//Not req
Entry entry = (Entry) itr.next();
Map<String, Object> map = (Map<String, Object>) entry.getValue();
map.clear();
itr.remove();
break;
}
}
Object object = objectFactory.getObject();
viewMap.put(name, object);
return object;
}
}
答案 2 :(得分:4)
这可能是一个错误。老实说,Seam 3的实现并不是那么好,CODI(以及DeltaSpike中的内容)要好得多。