工程的总体结构如下:
1. pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.johnfnash.learn</groupId>
<artifactId>config-listen</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>config-listen Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<springframework.version>4.1.4.RELEASE</springframework.version>
</properties>
<dependencies>
<!-- spring相关包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>config-listen</finalName>
</build>
</project>
2. 监听器类
继承自 Spring的 ContextLoaderListener
package com.johnfnash.learn.config.listener;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
/**
*配置文件更改listener
*/
public class ConfigModifyListener extends ContextLoaderListener {
/** 配置文件的相对路径 */
private final static String FILE_NAME = "/configue.properties";
/** 文件更改时间 */
private long modifyTime = 0;
/** 文件 */
private File file;
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
String rootPath = getClass().getResource("/").getFile().toString();
System.out.println("curr: " + rootPath);
file = new File(rootPath + "/" + FILE_NAME);
//启动定时任务
new Timer().schedule(new CheckFileTask(), 10000, 20000);
}
/**
* 检测文件更新任务
*/
class CheckFileTask extends TimerTask {
public void run() {
long time = file.lastModified();
System.out.println("time: " + time);
if(0 == modifyTime) {
modifyTime = time;
} else if(time != modifyTime) {
//文件被改动,在此可以做出相应操作
System.out.println("File has been changed!");
modifyTime = time;
}
}
}
}
其中配置文件放在 src/main/resources下
3. web.xml
这里需要将新建的ConfigModifyListener随web项目一起启动
<!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>
<!--加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--设置一起动当前的Web应用,就加载Spring,让Spring管理Bean-->
<listener>
<listener-class>
com.johnfnash.learn.config.listener.ConfigModifyListener
</listener-class>
</listener>
</web-app>