2017-05-22 68 views
0

這是一個更普遍的Spring問題,不一定是Spring社交Facebook問題;然而,我在這裏問這個問題,因爲我不清楚Spring是如何管理傳遞給控制器​​構造函數的Facebook引用的。管理控制器內的Facebook對象

我在春季啓動應用程序版本1.5.3.RELEASE中運行此。

我搜索了很多關於範圍代理如何工作以及控制器是否是單身人士等等的東西,但是對於這個特殊的用例,我仍然不清楚。我擔心兩個或多個同時發生的請求是否會導致一個引用干擾另一個引用。在我看來,不管範圍代理解決方案如何,都會有衝突。爲了解決這個問題,我將Facebook和ConnectionRepository對象注入包裝在Provider類中的控制器構造函數中。

@Inject 
public CopyGroupController(
     Provider<Facebook> facebook, 
     Provider<ConnectionRepository> connectionRepository) { 

它似乎注入了有意義的數據;但嘗試運行這段代碼時:

@PostConstruct 
public void init() { 
    ConnectionRepository repo = connectionRepository.get(); 
    this.userManager.createOrGetUser(
      repo.findPrimaryConnection(Facebook.class)); 

} 

出現此故障:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 

這創造了更多的不確定性,因爲我認爲FacebookTemplate正在設法爲請求或會話範圍的bean,它會注入作爲提供程序,因此該對象將駐留在ThreadLocale上;所以兩個或多個併發請求不會發生衝突。在控制器單例構造函數中傳遞請求作用域bean對我來說也沒有任何意義。所以我很困惑爲什麼用戶特定的引用甚至會被傳遞給一個只能被調用一次的構造函數。

我試着在構造函數中設置斷點來驗證是否存在衝突;但是會發生與上面相同的錯誤。有人可以向我解釋這是否是一個問題,如果是這樣的話;如何以最好,最現代的方式來解決它。

任何援助將不勝感激。 預先感謝您。

回答