在Spring中销毁的Session上的侦听器类中调用服务层函数 [英] call a service layer function in Listener class on Session destroyed in Spring

查看:156
本文介绍了在Spring中销毁的Session上的侦听器类中调用服务层函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.xml中配置了一个侦听器

I have one listener configured in web.xml

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

MyListener.java

具有以下代码

 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.

在Mylistener上调用服务层需要做哪些其他映射,在上面的代码中它根本不起作用

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

推荐答案

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).

这篇关于在Spring中销毁的Session上的侦听器类中调用服务层函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆