Java Spring HttpSessionListener陷入循环
我在我的项目中创建了一个HttpSessionListener。在这个侦听器中,我在会话中设置了如下内容:Java Spring HttpSessionListener陷入循环,java,spring,spring-mvc,httpsession,Java,Spring,Spring Mvc,Httpsession,我在我的项目中创建了一个HttpSessionListener。在这个侦听器中,我在会话中设置了如下内容: public void sessionCreated(HttpSessionEvent se) { //some Business for access to subsiteId se.getSession().setAttribute("subsiteId", subsiteId); //set some atribute for Statistic Mode
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(我的统计服务扩展了它)中,我想阅读我在会话列表中设置的subsitied属性:
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getAttribute("subsiteId")
但我的应用程序陷入了循环。再回到我的听众身边!
我想我可以在会话完全创建之前访问它。
有什么想法吗?最后,我通过在sessionCreated中设置统计模型的属性并在SessionStored中保存统计模型来解决这个问题:
最后,我通过在sessionCreated中设置统计模型的属性并在sessionDestroyed中保存统计模型来解决这个问题:
为什么让您的服务依赖于web,而现在您的整个应用程序都绑定到web层?只需将id传递到服务方法中。关于侦听器,
RequestContextHolder
仅在DispatcherServlet
看到请求后才填充,如果太迟,请注册一个RequestContextListener
,该侦听器在创建请求后立即执行,并在您自己的侦听器之前注册。监听器是按照其定义的顺序执行的,因此顺序很重要。为什么让您的服务依赖于web,而现在您的整个应用程序都绑定到web层?只需将id传递到服务方法中。关于侦听器,RequestContextHolder
仅在DispatcherServlet
看到请求后才填充,如果太迟,请注册一个RequestContextListener
,该侦听器在创建请求后立即执行,并在您自己的侦听器之前注册。侦听器是按照它们在中定义的顺序执行的,因此顺序很重要。
@Override
public void sessionCreated(HttpSessionEvent se) {
//set some atribute for Statistic Model
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
//save Statistic Model
}