Spring依赖注入servlet会话监听器
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配置文件(注:如果用的是spring注解就不需要这一步了)
<?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.yiibai.common.CounterService" /> </beans>
2. WebApplicationContextUtils
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);
}
}
如果是用的spring注解,并没有手动声明bean的名称,则用CounterService counterService = (CounterService) ctx.getBean("CounterService");来实现
3. 集成
- 注册“ContextLoaderListener”作为第一个监听器,使Web应用程序知道Spring上下文加载程序。
- 配置“contextConfigLocation”,并定义Spring的bean配置文件。
File : 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.yiibai.common.SessionCounterListener
</listener-class>
</listener> <servlet>
<servlet-name>Spring DI Servlet Listener</servlet-name>
<servlet-class>com.yiibai.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.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依赖注入servlet会话监听器的更多相关文章
- java线程中如何使用spring依赖注入
实现一个线程继承了Thread或实现Runnable接口,想在run方法中使用spring依赖注入(操作数据库),此时报错为空指针,就是说没有注入进来. 实验了几种方式,分别说一下优缺点. 1:写了工 ...
- Spring依赖注入(IOC)那些事
小菜使用Spring有几个月了,但是对于它的内部原理,却是一头雾水,这次借着工作中遇到的一个小问题,来总结一下Spring. Spring依赖注入的思想,就是把对象交由Spring容器管理,使用者只需 ...
- Spring依赖注入三种方式详解
在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...
- Spring依赖注入:注解注入总结
更多11 spring 依赖注入 注解 java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...
- Spring 依赖注入,在Main方法中取得Spring控制的实例
Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...
- Spring依赖注入 --- 简单使用说明
Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...
- Spring依赖注入 --- 模拟实现
Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...
- Java Web系列:Spring依赖注入基础
一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是 ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
随机推荐
- Unreal Engine 4 反射机制的实现
转自:http://blog.csdn.net/noahzuo/article/details/51482255 反射机制 反射机制指的是程序可以在运行期间进行检测和修改自己状态的能力. UE4引擎使 ...
- 解决“Can't bind to local 8630 for debugger”错误--查杀多余进程
Can't bind to local 8630 for debugger 表明本地8630端口被占用 1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\& ...
- SparkStreaming 的编程模型
依赖管理 基本套路 Dstream输入源 ---input DStream Dstream输入源--- Receiver 内置的input Dstream : Basic Source 内置的inpu ...
- 大话java性能优化 pdf 下载(全本)
扫加公众号,回复”大话java性能优化",免费获取此书.
- 25. LiveBos调用class实例
var v= ABS_LOADBEAN('com.apex.mmsqljdbc.TestJDBC');var date=ABS_SQLVALUE("select to_char(?,'yyy ...
- java Export Excel POI 转
最终选择用POI成功导出excel.总之很有用. http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html http://poi ...
- 4 并发编程-(进程)-守护进程&互斥锁
一.守护进程 主进程创建子进程,然后将该进程设置成守护自己的进程,守护进程就好比崇祯皇帝身边的老太监,崇祯皇帝已死老太监就跟着殉葬了. 关于守护进程需要强调两点: 其一:守护进程会在主进程代码执行结束 ...
- python内置函数 eval()、exec()以及complie()函数
1.eval函数 eval() 函数用来执行一个字符串表达式,并返回表达式的值. eval(expression[, globals[, locals]]) 参数 expression -- 表达式. ...
- iperf点对点网络性能测试工具
什么是Iperf?Iperf 是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性.Iperf可以报告带宽,延迟抖动和数据包丢失 ...
- chattr与lsattr命令
这两个命令是用来查看和改变文件.目录属性的,与chmod这个命令相比,chmod只是改变文件的读写.执行权限,更底层的属性控制是由chattr来改变的. chattr命令的用法:chattr [ -R ...