在我們的應用中,所有的REST API的形式爲:訪問數據
HTTP:// {上下文}/{PRODUCT_ID}/{rest_url_path}
我必須通過從數據庫中獲取product_id的ProductDetails
來驗證Spring Security Filter/SpringMVC攔截器中的{product_id}。 ProductDetails
將在Spring控制器/服務類中使用。
我不想在控制器/服務中再次獲取ProductDetails
。所以我想爲RequestScope
某處存儲ProductDetails
對象。
我有3種方法。但每個人都有自己的優點和缺點。請讓我知道哪一個更好。另外建議任何替代方法。
方法-1:
保存請求屬性內的ProductDetails
對象。 內部控制器,我可以很容易地得到HttpRequest
。內部服務,我可以得到HttpRequest
通過:
@Autowired
HttpServletRequest request;
或
RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = null;
if (attribs instanceof ServletRequestAttributes) {
request = ((ServletRequestAttributes) attribs).getRequest();
}
但我不希望有內部服務的HTTP請求的依賴做出代碼WebLayer邏輯更加獨立。
方法2:
使用任何在內存中緩存基於product_id
存儲ProductDetails
但是,這我覺得只有殺了這個用例。增加緩存不必要的依賴關係。
方法-3:
貯存於ThreadLocal
變量來存儲請求範圍數據的對象。 但不知道這是否正確。
讓我知道,一個有效的辦法來解決這個問題
欲瞭解更多信息,你可以檢查https://stackoverflow.com/questions/35444633/spring-how-to-pass-objects-from-filters-to-controllers。這是一種類似的用例。 –
按照上面這個線程,我猜'ThreadLocal'似乎很有效率。還有關於使用'RequestScope Bean'的說法。這看起來更有效率。我必須試一試 – Tushar