2016-02-10 24 views
1

我在我的項目中創建了一個HttpSessionListener。在這個監聽器,我將在會議上類似下面的東西:Spring HttpSessionListener屬於循環

public void sessionCreated(HttpSessionEvent se) { 
    //some Business for access to subsiteId 
    se.getSession().setAttribute("subsiteId", subsiteId); 

    //set some atribute for Statistic Model 
    iStatisticService.save(remoteIp, userAgent, page); 
} 

在我GenericSave(我StatisticService擴展它),我想讀subsiteId 屬性我在SessionListener已經設置:

((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getAttribute("subsiteId") 

但我的應用程序屬於循環。並再次回到我的聽衆! 我想我在完全創建之前訪問Session。 任何想法?

+1

爲什麼你讓你的服務所依賴的網絡,現在你的整個應用程序上綁定到web層?只需將該ID傳入服務方法即可。關於監聽器,'RequestContextHolder'只在DispatcherServlet看到請求後才被填充,如果這太遲了,就註冊一個'RequestContextListener',一旦請求被創建就會執行它,並在自己的監聽器中註冊它。聽衆按照他們定義的順序執行,因此排序很重要。 –

回答

0

最後,我解決了在統計模型的一套屬性這個問題sessionCreated和保存統計模型sessionDestroyed

@Override 
public void sessionCreated(HttpSessionEvent se) { 
    //set some atribute for Statistic Model 
} 

@Override 
public void sessionDestroyed(HttpSessionEvent se) { 
    //save Statistic Model 
}