我有一個singleton spring bean被調用以響應某些客戶端操作。 我希望捕獲有關客戶端的一些信息(特別是IP地址)。我認爲這個信息的最佳來源是請求對象。如何從我的bean中獲取對請求對象的訪問權限?如何從一個singleton spring bean中訪問一個ServletRequest?
請原諒我,如果這是一個明顯的問題,我對Spring很陌生。
我試過一兩件事沒有成功:
((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
.getRequest().getRemoteAddr();
但是,隨着一個IllegalStateException失敗了的currentRequestAttributes
。唯一的例外文本建議使用RequestContextListener
或RequestContextFilter
我發現一個reference如何配置RequestContextListener
,但我仍然不知道改變我的豆,所以我可以訪問的請求信息。
完美,這確實工作!我剛剛在web.xml中有了RequestContextListener的名稱。 requestDestroyed(ServletRequestEvent).attributes做了什麼? – wolfcastle 2010-09-02 20:22:38
正確,這不是Spring MVC應用程序。對不起,我第一次看到你的答案沒有看到。 – wolfcastle 2010-09-02 20:29:34