1
A
回答
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;
相關問題
- 1. 獲取在Spring中實現通用接口的所有bean
- 2. 獲取所有CDI bean
- 3. 如何迭代並獲取會話中的所有用戶名
- 4. InternetExplorer | IE.au3獲取會話的所有cookie
- 5. Spring獲取所有@Entity類
- 6. 獲取所有PHP會話列表session_id
- 7. 獲取所有用戶的所有約會或獲得一個房間的所有約會
- 8. 從LDAP獲取所有用戶
- 9. Larvael等效會話所有用戶
- 10. 獲取Active Directory中的所有用戶
- 11. Spring不從JAR導入所有bean實現
- 12. 打印加載的所有Spring bean
- 13. VSTO - 獲取所有帳戶
- 14. 獲取所有用戶的所有云端硬盤文件
- 15. 從所有登錄的瀏覽器中刪除用戶會話
- 16. 從特定羣組中的所有用戶獲取用戶名
- 17. Spring框架default-lazy-init所有bean
- 18. 獲取所有用戶的last_login?
- 19. 獲取OS X上的所有用戶
- 20. 使用grah api獲取用戶所屬的所有租戶
- 21. 從會話導軌獲取所有數據
- 22. ASP.NET WebApi 2獲取服務器中的所有活動會話(用戶)
- 23. 從CNContactStore獲取所有CNGroup
- 24. 從SQLite獲取所有行
- 25. 使用有狀態會話Bean來跟蹤用戶的會話
- 26. 如何獲取symfony中的所有會話數據?
- 27. 獲取彈簧安全中的所有活動會話
- 28. 所有對話框更新支持bean
- 29. 使用GroupPrincipal從Active Directory中的組獲取所有用戶
- 30. GData和iOS從所有日曆中獲取所有事件?