我尝试在applicationListener impl中获取sessionId。它在session.getId()中抛出NULLPointerException。我想知道为什么它会例外。会话已创建,但无法访问其他属性。以下是我的代码。
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {
@Autowired
HttpSession session;
@Override
public void onApplicationEvent(final QuotePricingEvent quoteEvent) {
LOGGER.error("Session id: {}, userid: {}", session.getId());
}
}
修改
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(RequestContextListener.class);
}
答案 0 :(得分:-1)
请确保您在RequestContextListener
中注册了web.xml
,以便Spring可以注入当前HTTPSession
。
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>