Spring 監聽session 失效方法


public class SessionCounter implements HttpSessionListener {
 
 

 private static int activeSessions =0; 
 /* Session創建事件 */ 
 public void sessionCreated(HttpSessionEvent event) {

//創建session

  
 } 
 /* Session失效事件 */ 
 public void  sessionDestroyed(HttpSessionEvent se){ 
  //Spring注解無法注入Session監聽器解決辦法  
  ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(se.getSession().getServletContext());
  SystemlogService memberService = (SystemlogService) ctx.getBean("systemlogServiceImpl"); // 填寫要注入的類,注意第一個字母小寫
  User username=(User) se.getSession().getAttribute("user");
  if(username!=null){
   Systemlog systemlog = new Systemlog();
   String userName2 = username.getUserName();
   systemlog.setOperator(userName2);
     memberService.insertSelective(systemlog);
  }
   } 
 

}

web.xml中

  <listener>
  <listener-class>
  com.secure.listener.SessionCounter
  </listener-class>
 </listener>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM