问题
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