2016-11-14 112 views
-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); 
    } 
+0

請提供[mcve]和完整的堆棧跟蹤。 –

回答

-1

請確保您已註冊在web.xml使Spring能夠注入電流HTTPSession一個RequestContextListener

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
+0

我在AbstractAnnotationConfigDispatcherServletInitializer impl中添加了RequestContextListener。 – swemon