Look at the code first
PackageCom.oa.listener;Importjavax.servlet.http.HttpSession;Importjavax.servlet.http.HttpSessionEvent;ImportJavax.servlet.http.HttpSessionListener;Importorg.springframework.beans.factory.annotation.Autowired;ImportCom.oa.service.SystemManagementService;/** * * @author: Zhao * @time: May 3, 2016 * @description: Session listener, when the session is destroyed, perform database operations. Change the online status of User_login **/ Public classSessionlistenerImplementsHttpsessionlistener {@AutowiredPrivateSystemmanagementservice Service; @Override Public voidsessioncreated (Httpsessionevent event) {HttpSession session=event.getsession (); System.out.println ("Session:" +Session.hashcode ()); } @Override Public voidsessiondestroyed (Httpsessionevent event) {HttpSession session=event.getsession (); String login_id= (String) session.getattribute ("id"); if(login_id! =NULL&& "". Equals (login_id)) { //Change Online StatusSession.setattribute ("id", "" "); Service.dologinandlogout (login_id,false); } System.out.println (The session listener runs ... "); }}
This column is very good, the interface of the business logic layer through spring automatic assembly to inject, and then use on it. Results found always error. Just say I have a blank pointer and look for it, I didn't write anything but beans.
Workaround, without annotating those things, we get applicationcontext through the context and then get the bean through the bean name.
@Override Public voidsessiondestroyed (Httpsessionevent event) {HttpSession session=event.getsession (); String login_id= (String) session.getattribute ("id"); if(login_id! =NULL&& "". Equals (login_id)) { //Change Online StatusSession.setattribute ("id", "" "); ApplicationContext Context=Webapplicationcontextutils.getrequiredwebapplicationcontext (Session.getservletcontext ()); Systemmanagementservice Service= (Systemmanagementservice) context.getbean ("Systemmanagementservice"); Service.dologinandlogout (login_id,false); } System.out.println (The session listener runs ... "); }
Spring Auto-Assembly fails in session listener