2016-01-13 35 views
1

我需要一個特定於請求的對象,並在Web應用程序中使用同一個對象來獲取/設置值以執行特定請求的業務邏輯。我可以使用request.setAttribute/request.getAttribute或者我應該使用ThreadLocal。我的理解是,每個請求都由線程池中的單獨線程處理。我有這樣一個Spring的web Application.I還可以看到一個RequestContextHolder,不知道這將是合適人選HttpServletRequest request.setAttribute vs ThreadLocal vs RequestContextHolder

+0

http://stackoverflow.com/questions/10096483/is-threadlocal-preferable-to-httpservletrequest-setattributekey-value – Kidburla

回答

0

看來,你想要一個應用廣泛的屬性來提供給每一個具體的要求。爲此,您可以使用request.getServletContext()。setAttribute()和request.getServeltContext()。getAttribute()。 ServletContext表示應用程序,因此使用它設置的屬性在應用程序的整個生命週期中都可用,並且可以被多個請求使用。

+0

需要請求特定對象而不是應用程序範圍 – Bukhtawar

+0

請澄清問題,你說「.. ..並在整個Web應用程序中使用相同的對象來獲取/設置值......「。 – mmulholl

+0

我指的是一個對象,用於跨不同Web組件的一個請求線程(請求特定對象)的全部執行 – Bukhtawar

相關問題