Java 在spring ApplicationListener中注入httpSession
我尝试在applicationListener impl中获取sessionId。它在session.getId()中抛出NULLPointerException。我想知道为什么会有例外。会话已创建,但无法访问其他属性。下面是我的代码Java 在spring ApplicationListener中注入httpSession,java,spring,Java,Spring,我尝试在applicationListener impl中获取sessionId。它在session.getId()中抛出NULLPointerException。我想知道为什么会有例外。会话已创建,但无法访问其他属性。下面是我的代码 @Component public class MyEventListener implements ApplicationListener<MyEvent> { @Autowired HttpSession se
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {
@Autowired
HttpSession session;
@Override
public void onApplicationEvent(final QuotePricingEvent quoteEvent) {
LOGGER.error("Session id: {}, userid: {}", session.getId());
}
}
请确保您已在
web.xml
中注册了RequestContextListener
,以便Spring可以注入当前的HTTPSession
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
org.springframework.web.context.request.RequestContextListener
请提供一个完整的堆栈跟踪。我在AbstractAnnotationConfigDispatcherServletInitializer impl中添加了RequestContextListener。
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>