Spring Auto-Assembly fails in session listener

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.