call a service layer function in Listener class on Session destroyed in Spring

我与影子孤独终老i 提交于 2019-12-24 20:46:34

问题


I have one listener configured in web.xml

<listener>
      <listener-class>com.Mylistener</listener-class>
</listener>

MyListener.java

has following code

 public class MyListener extends HttpSessionEventPublisher{

           myServiceInterface myService;

           @Override
           public void sessionCreated(HttpSessionEvent event) {     
              super.sessionCreated(event);
           }

           @Override
           public void sessionDestroyed(HttpSessionEvent event) {
             //Call a method from service layer which is communicating with DAO layer and then database.
    super.sessionDestroyed(event);
           }

    }

That service already had its own mapping and working fine.

what other mappings, i need to do to call service layer in Mylistener, above code it not working at all


回答1:


public class MyListener extends HttpSessionEventPublisher {
    @Override
    public void sessionDestroyed(final HttpSessionEvent event) {
        final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getSession().getServletContext());
        final MyServiceInterface service = ctx.getBean(MyServiceInterface.class);
        // use service
        super.sessionDestroyed(event);
    }
}    

Should work (not tested).



来源:https://stackoverflow.com/questions/13836310/call-a-service-layer-function-in-listener-class-on-session-destroyed-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!