1. 程式人生 > >Spring 監聽session 失效方法

Spring 監聽session 失效方法

con eve spring註解 無法 nco clas textutils creat 監聽器

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>

Spring 監聽session 失效方法