Springには、 ”
ContextLoaderListener
“リスナーが付属しており、セッションリスナーへのSpring依存性注入を有効にします。このチュートリアルでは、セッションリスナーにBeanにSpring依存性インジェクションを追加することによって、このリンク://servlet/a-simple-httpsessionlistener-example-active-sessions-counter/[HttpSessionListenerの例]を改訂します。
1. Spring Beans
作成されたセッションの合計数を出力する簡単なカウンタサービスを作成します。
ファイル:Counter Service.java
package com.mkyong.common; public class CounterService{ public void printCounter(int count){ System.out.println("Total session created : " + count); } }
File:counter.xml
– Bean構成ファイル。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="counterService" class="com.mkyong.common.CounterService"/> </beans>
2. WebApplicationContextUtils
Springのコンテキストを取得するために “` `WebApplicationContextUtils`”を使用し、その後、通常のSpringの方法で宣言されたSpringのBeanを取得できます。
ファイル:セッションカウンタListener.java
package com.mkyong.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”ファイルの中にあります。
-
最初のリスナーとして「ContextLoaderListener`」を登録して、
WebアプリケーションはSpringコンテキストローダを認識します。
-
“contextConfigLocation”を設定し、あなたのSpringのbeanを定義してください
構成ファイル。
ファイル:web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/Spring/counter.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> com.mkyong.common.SessionCounterListener </listener-class> </listener> <servlet> <servlet-name>Spring DI Servlet Listener</servlet-name> <servlet-class>com.mkyong.common.App</servlet-class> </servlet> <servlet-mapping> <servlet-name>Spring DI Servlet Listener</servlet-name> <url-pattern>/Demo</url-pattern> </servlet-mapping> </web-app>
File:App.java
package com.mkyong.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", "mkyong.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/SpringWebExample/Demo
」にアクセスします。
出力
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”は、ほとんどすべてのWebアプリケーション** にSpring依存性注入を統合する一般的な方法です。
ダウンロード
ダウンロードする – リンク://wp-content/uploads/2010/03/SpringWeb-DI-Session-listener-Example.zip[SpringWeb-DI-Session-listener-Example.zip]