Spring依賴注入servlet會話監聽器

Spring提供了一個 「ContextLoaderListener」 監聽器,以使 Spring 依賴注入到會話監聽器。 在本教程中,通過添加一個 Spring 依賴注入一個bean 到會話監聽器修改 HttpSessionListener 例子。

1. Spring Beans

創建一個簡單的計數服務來打印創建的會話總數。

File : CounterService.java

package com.yiibai.common;

public class CounterService{

public void printCounter(int count){
    System.out.println("Total session created : " + count);
}

}

File : counter.xml – Bean配置文件

<bean id="counterService" class="com.yiibai.common.CounterService" />

2. WebApplicationContextUtils

使用「WebApplicationContextUtils」來獲得 Spring 上下文,以後可以得到任何聲明Spring的Bean 在一個正常的 Spring 方式。

File : SessionCounterListener.java

package com.yiibai.common;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SessionCounterListener implements HttpSessionListener {

 private static int totalActiveSessions;

 public static int getTotalActiveSession(){
       return totalActiveSessions;
 }

@Override
public void sessionCreated(HttpSessionEvent arg0) {
       totalActiveSessions++;
       System.out.println("sessionCreated - add one session into counter");    
       printCounter(arg0);
}

@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
       totalActiveSessions--;
       System.out.println("sessionDestroyed - deduct one session from counter");    
       printCounter(arg0);
}    

private void printCounter(HttpSessionEvent sessionEvent){

      HttpSession session = sessionEvent.getSession();

      ApplicationContext ctx = 
            WebApplicationContextUtils.
                  getWebApplicationContext(session.getServletContext());

      CounterService counterService = 
                  (CounterService) ctx.getBean("counterService");

      counterService.printCounter(totalActiveSessions);
}

}

3. 集成

唯一的問題是,如何使 Web 應用程序知道在哪裏可以加載 Spring bean 配置文件?祕訣是在「web.xml」文件中。

  1. 註冊「ContextLoaderListener」作爲第一個監聽器,使Web應用程序知道Spring上下文加載程序。

  2. 配置「contextConfigLocation」,並定義Spring的bean配置文件。

File : web.xml

Archetype Created Web Application contextConfigLocation /WEB-INF/Spring/counter.xml org.springframework.web.context.ContextLoaderListener com.yiibai.common.SessionCounterListener Spring DI Servlet Listener com.yiibai.common.App Spring DI Servlet Listener /Demo

File : App.java

package com.yiibai.common;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class App extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{

    HttpSession session = request.getSession(); //sessionCreated() is executed
    session.setAttribute("url", "yiibai.com"); 
    session.invalidate();  //sessionDestroyed() is executed

    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body>");
    out.println("<h1>Spring Dependency Injection into Servlet Listenner</h1>");
    out.println("</body>");
    out.println("</html>");    

}

啓動Tomcat,並訪問 URL 「http://localhost:8080/7.2-SpringDemo/"

輸出結果

sessionCreated - add one session into counter
Total session created : 1
sessionDestroyed - deduct one session from counter
Total session created : 0

看看控制檯的輸出,會得到通過 Spring DI 記數服務的 bean,並打印會話的總數。

總結

在Spring中,「ContextLoaderListener」是一個通用的方法集成Spring依賴注入到幾乎所有的Web應用程序。

下載代碼 –   http://pan.baidu.com/s/1bpmhQY