我有以下問題,我配置了以下應該存儲在會話中的類。Spring MVC 3.0訪問會話變量
<bean id="Users" class="com.doolloop.DlUser" scope="session">
<aop:scoped-proxy/>
</bean>
然後我在調度員的servlet我想訪問該類用戶並設置
@RequestMapping(value="/authenticate.do",method = RequestMethod.POST)
public String sampleAuthentication(@Valid Person person, BindingResult result,
Map model,HttpServletRequest request){
...... /some code
HttpSession session = request.getSession();
DlUser user = (DlUser) session.getAttribute("Users");
/// some uses for user object
}
的問題是,我一直都想與用戶對象的空值。
我在做什麼錯?
第二個問題,我在文章中看到,訪問HttpSession
不是線程安全的,它怎麼能做到安全呢?應該是種Singleton?爲什麼這不是線程安全操作?
預先感謝您。
Danny。
這是否意味着這個bean將存在與Http會話存在相同的時間? – 2010-10-23 23:17:14
是的,這是正確的,但請閱讀Bozho的答案。如果你將這個bean注入一個更長壽的bean,效果將不會像你期望的那樣。你最好使用session.getAttribute()來獲取對象,或者使用AOP爲會話對象進行代理 - 請看這裏的例子以獲取更多細節:http://wheelersoftware.com/articles/spring-session-scoped -beans.html。本質上,這個鏈接解釋瞭如何爲會話範圍的bean創建一個代理,所以代理就是獲得匹配會話內容的正確實例的代理 – 2010-10-24 00:42:27