2010-05-04 39 views

回答

1

我不做春天,但在正常JSF/JSP/Servlet的,你会抓住HttpSessionBindingListener这一点。基本上,您需要给会话范围bean一个static List<Bean>属性并相应地实现该接口,以更新valueBound()valueUnbound()方法中的static列表。

您可以在this answer找到详细的代码示例。

0

这里是我想出了一个解决方案利用弹簧:

我进行正常的春季单例的bean称为SessionBeanHolder。 这个bean拥有我的会话bean的列表。 当用户登录时,我将会话bean添加到我的SessionBeanHolder中。

在Spring中引用会话bean时,实际上是指代理。 因此,使这项工作的关键是获取底层bean以添加到SessionBeanHolder中。

下面是示例代码:

注:我的会话bean被称为SessionInfo。

@Scope(value="singleton") 
@Component 
public class SessionBeanHolder { 

    static Set<SessionInfo> beans; 

    public SessionBeanHolder() { 
     beans = new HashSet<SessionInfo>(); 
    } 

    public Collection<SessionInfo> getBeans() { 
     return beans; 
    } 

    public void addBean(SessionInfo bean) { 
     try { 
      this.beans.add(removeProxyFromBean(bean)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    // Fetch the underlying bean that the proxy refers to 
    private SessionInfo removeProxyFromBean(SessionInfo proxiedBean) { 
     if (proxiedBean instanceof Advised) { 
      try { 
       return (SessionInfo) ((Advised) proxiedBean).getTargetSource().getTarget(); 
      } catch (Exception e) { 
       throw new RuntimeException(e); 
      } 
     } else { 
      return proxiedBean; 
     } 
    } 
} 

当然,只要你想添加的会话Bean或者获取所有Bean的列表,只需在自动装配和SessionBeanHolder使用它的方法。

@Autowired 
    SessionBeanHolder sessionBeanHolder;