2016-11-14 15 views
-2

私はapplicationListener impl内でsessionIdを取得しようとしています。 session.getId()にNULLPointerExceptionをスローします。なぜそれが例外になったのだろうか。セッションは作成されますが、他の属性にはアクセスできません。以下は私のコードです。Spring ApplicationListenerでhttpSessionを挿入します。

@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

春は現在HTTPSessionを注入できるように、あなたのweb.xmlRequestContextListenerが登録されていることを確認してください。

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

AbstractAnnotationConfigDispatcherServletInitializer implにRequestContextListenerを追加しました。 – swemon

関連する問題