在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等。要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个Listener中启动一个线程,在线程里面实现功能。

1. 自定义Listener

在Struts+Spring+Hibernate的Web项目中,web.xml里面一般都会有这样的代码:

    <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这几句代码使得Web项目的容器(也就是Web服务器,比如Tomcat)在项目启动时实例化了一个org.springframework.web.context.ContextLoaderListener类。

类似的,我们也可以在web.xml里面自己定义一个Listener,让Web服务器去实例化:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <listener>
<listener-class>com.XXX.listener.WSListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>

在以上的web.xml文件中,我们就让Web服务器在启动时实例化我们自己定义的com.XXX.listener.WSListener类(一般自己定义的Listener类要写在org.springframework.web.context.ContextLoaderListener的后面),然后在该类中去启动线程:

public class WSListener implements ServletContextListener{

    private WSThread wsThread;

    @Override
public void contextDestroyed(ServletContextEvent event) {
// TODO Auto-generated method stub
if (wsThread != null && wsThread.isRunning){
wsThread.stopThread();
}
} @Override
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
if (wsThread == null){
wsThread = new WSThread(event);
wsThread.start();
}
} }

Listener类是由Web服务器管理的,当Web服务器启动时,将Listener类实例化并调用其contextInitialized(ServletContextEvent event)方法,当Web服务器关闭时,调用其contextDestroyed(ServletContextEvent event)方法,因此我们可以分别在这两个方法里面实现线程的启动和结束。

2. 在Spring容器以外获得其内部的Bean的实例的引用

被启动的线程用于间隔一定的时间扫描一次数据库,找出新增加的数据。在一般的Struts+Spring+Hibernate的Web项目中,Spring容器中的Bean是由Spring容器管理的,而我们这里启动的线程并不在Spring容器中,那么怎样获得Spring容器中Bean的实例的引用进而访问数据库呢?可以使用Spring的WebApplicationContextUtils工具类,该工具类获得Spring容器的引用,再获得其内部的Bean的实例的引用。

线程的代码:

public class WSThread extends Thread{
public volatile boolean isRunning = true;
// 两次扫描之间休眠的时间
public long s_time;
private WebApplicationContext context;
private PropService propService;
private Prop prop;
private Temp2Service temp2Service;
private Temp2 temp2; private TempService tempService; ServletContextEvent event;
public WSThread(ServletContextEvent e){
this.event = e;
this.context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
this.propService = (PropService) context.getBean("propService");
this.temp2Service = (Temp2Service) context.getBean("temp2Service");
} public void run(){
while (isRunning){
try {
this.prop = propService.findByName("scan_time");
this.s_time = Integer.parseInt(prop.getValue())*1000;
sleep(s_time);
System.out.println("Run!!!!!!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public void stopThread(){
isRunning = false;
} public boolean isRunning(){
return isRunning;
} }

在该线程的构造函数中,使用了从Listener传过来的ServletContextEvent变量,用该变量的getServletContext()方法,获取Web项目的servletContext,然后再以这个ServletContext作为参数,使用WebApplicationContextUtils的getRequiredWebApplicationContext()方法获取ApplicationContext对象,最后再通过ApplicationContext的getBean()方法获取到Bean的实例,实现对数据库的访问。

Struts+Spring+Hibernate项目的启动线程的更多相关文章

  1. Struts+Spring+Hibernate项目整合AJAX+JSON

    1.什么是AJAX AJAX是 "Asynchronous JavaScript and XML" 的简称,即异步的JavaScript和XML. 所谓异步,就是提交一个请求不必等 ...

  2. Struts+Spring+Hibernate整合入门详解

    Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者:  Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念       St ...

  3. 用eclipse搭建SSH(struts+spring+hibernate)框架

    声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...

  4. Struts+Spring+Hibernate、MVC、HTML、JSP

    javaWeb应用 JavaWeb使用的技术,比如SSH(Struts.Spring.Hibernate).MVC.HTML.JSP等等技术,利用这些技术开发的Web应用在政府项目中非常受欢迎. 先说 ...

  5. velocity+spring mvc+spring ioc+ibatis初试感觉(与struts+spring+hibernate比较)

    velocity+spring mvc+spring ioc+ibatis框架是我现在公司要求采用的,原因是因为阿里巴巴和淘宝在使用这样的框架,而我公司现在还主要是以向阿里巴巴和淘宝输送外派人员为 主 ...

  6. SSH(struts+spring+hibernate)常用配置整理

    SSH(struts+spring+hibernate)常用配置整理 web.xml配置 <?xml version="1.0" encoding="UTF-8&q ...

  7. Struts,spring,hibernate三大框架的面试

    Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...

  8. Struts+Spring+Hibernate进阶开端(一)

    入行就听说SSH,起初还以为是一个东西,具体内容就更加不详细了,总觉得高端大气上档次,经过学习之后才发现,不仅仅是高大上,更是低调奢华有内涵,经过一段时间的研究和学习SSH框架的基本原理与思想,总算接 ...

  9. 配置spring的监听器 让spring随项目的启动而启动

    <!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...

随机推荐

  1. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  2. vs15 preview5 离线安装包

    1.介绍 vs15是微软打造的新一代IDE,全新的安装方式.官网介绍如下(https://blogs.msdn.microsoft.com/visualstudio/2016/10/05/announ ...

  3. 手机web如何实现多平台分享

    话说App一般都带有分享到社交平台的入口,web网页的分享也有很不错的框架,但是随着HTML5的不断发展,手机web页面越来越多的进入到我们的生活中,那如何在我们的手机上完成分享呢?话说各大分享平台都 ...

  4. 跟着老男孩教育学Python开发【第二篇】:Python基本数据类型

    运算符 设定:a=10,b=20 . 算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**3 ...

  5. Linux-Rhel6 恢复误删文件

    需要工具extundelete(可通过xshell上传下载到linux)知识点:ifconifig eth0             查看ipfdisk /dev/sda          管理磁盘m ...

  6. 程序员装B指南

    一.准备工作 "工欲善其事必先利其器." 1.电脑不一定要配置高,但是双屏是必须的,越大越好,能一个横屏一个竖屏更好.一个用来查资料,一个用来写代码.总之要显得信息量很大,效率很高 ...

  7. Xamarin体验:使用C#开发iOS/Android应用

    Xamarin是Mono创始人Miguel de Icaza创建的公司,旨在让开发者可以用C#编写iOS, Android, Mac应用程序,也就是跨平台移动开发.   简介 Xamarin是基于Mo ...

  8. 在 Linux 中使用 Git 及其 和 Eclipse 的集成

    ##参考资料## 我是通过阅读<Pro Git>这本书学习 Git 的,我读的时候还是第一版的英文版,现在已经出第二版了,而且英文版和中文版都有.英文第二版的地址是 [https://gi ...

  9. 2000条你应知的WPF小姿势 基础篇<51-56 依赖属性>

    前一阵子由于个人生活原因,具体见上一篇,耽搁了一阵子,在这里也十分感谢大家支持和鼓励.现在开始继续做WPF2000系列. 在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件 ...

  10. 让tomcat支持中文cookie

    这的确是一个不正常的需求,按照规范,开发者需要将cookie进行编码,因为tomcat不支持中文cookie. 但有时候,你不得不面对这样的情况,比如请求是由他人开发的软件,比如,浏览器控件发出的. ...