-2
我嘗試在applicationListener impl中獲取sessionId。它在session.getId()中引發NULLPointerException。我想知道爲什麼它有異常。會話已創建,但無法訪問其他屬性。以下是我的代碼。春天注入httpSession ApplicationListener
@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);
}
請提供[mcve]和完整的堆棧跟蹤。 –