一.spring容器都初始化完成之后做操作
packagecom.bijian.study.listener;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.ApplicationListener;importorg.springframework.context.event.ContextRefreshedEvent;importorg.springframework.web.context.support.XmlWebApplicationContext;importcom.bijian.monitor.FileMonitor;public class ContextFileListener implements ApplicationListener{
@AutowiredprivateFileMonitor fileMonitor;
@Overridepublic voidonApplicationEvent(ContextRefreshedEvent event) {if(event.getSource() instanceofXmlWebApplicationContext){if(((XmlWebApplicationContext)event.getSource()).getDisplayName().equals("Root WebApplicationContext")){try{
fileMonitor.monitor();
fileMonitor.start();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
二.applicationContext.xml添加配置
三.添加文件监听 FileMonitor、FileListener 可看资料commons-io-2.4
packagecom.bijian.study.monitor;importjava.io.File;importorg.apache.commons.io.monitor.FileAlterationListener;importorg.apache.commons.io.monitor.FileAlterationMonitor;importorg.apache.commons.io.monitor.FileAlterationObserver;public classFileMonitor {private FileAlterationMonitor monitor = null;privateString filePath;privateFileAlterationListener alterationListener;private long intervalTime = 10000L;public FileMonitor(long interval) throwsException {
monitor= newFileAlterationMonitor(interval);
}public FileMonitor() throwsException {
monitor= newFileAlterationMonitor(intervalTime);
}public voidmonitor(String path, FileAlterationListener listener) {
FileAlterationObserver observer= new FileAlterationObserver(newFile(path));
monitor.addObserver(observer);
observer.addListener(listener);
}public voidmonitor() {
FileAlterationObserver observer= new FileAlterationObserver(newFile(filePath));
monitor.addObserver(observer);
observer.addListener(alterationListener);
}public void stop() throwsException {
monitor.stop();
}public void start() throwsException {
monitor.start();
}publicString getFilePath() {returnfilePath;
}public voidsetFilePath(String filePath) {this.filePath =filePath;
}publicFileAlterationListener getAlterationListener() {returnalterationListener;
}public voidsetAlterationListener(FileAlterationListener alterationListener) {this.alterationListener =alterationListener;
}public longgetIntervalTime() {returnintervalTime;
}public void setIntervalTime(longintervalTime) {this.intervalTime =intervalTime;
}
}
packagecom.bijian.study.listener;importjava.io.File;importorg.apache.commons.io.monitor.FileAlterationListener;importorg.apache.commons.io.monitor.FileAlterationObserver;public class FileListener implementsFileAlterationListener {
@Overridepublic voidonStart(FileAlterationObserver observer) {//TODO Auto-generated method stub
System.out.println("onStart()");
}
@Overridepublic voidonDirectoryCreate(File directory) {//TODO Auto-generated method stub
System.out.println("onDirectoryCreate()");
}
@Overridepublic voidonDirectoryChange(File directory) {//TODO Auto-generated method stub
System.out.println("onDirectoryChange()");
}
@Overridepublic voidonDirectoryDelete(File directory) {//TODO Auto-generated method stub
System.out.println("onDirectoryDelete()");
}
@Overridepublic voidonFileCreate(File file) {//TODO Auto-generated method stub
System.out.println("onFileCreate()");
}
@Overridepublic voidonFileChange(File file) {//TODO Auto-generated method stub
System.out.println("onFileChange()");
}
@Overridepublic voidonFileDelete(File file) {//TODO Auto-generated method stub
System.out.println("onFileDelete()");
}
@Overridepublic voidonStop(FileAlterationObserver observer) {//TODO Auto-generated method stub
System.out.println("onStop()");
}
}
至此完成启动项目,在指定文件夹D:/SVN/下增加、修改、删除文件都会触发相关方法。
附:解决onApplicationEvent方法被执行两次以上的问题
原因:
在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。在一个类里面实现了ApplicationListener接口,用于在初始化完成后做一些事情,但是Debug或输出日志,发现它执行了3次,其中一次是Spring 框架初始化时执行,另外两次是在项目启动成功后,加载Spring-MVC时执行的。
解决方法:
1.根据对象值来告诉定时器只执行一次
通过打 breakpoint看到,这两次执行中,onApplicationEvent(ContextRefreshdEvent event)方法中的 event对象值不同,第一次是 Root WebApplicationContext , 据此可以根据对象值来告诉定时器只执行一次即可,如如上实例所示,在这我System.out.println输出((XmlWebApplicationContext)event.getSource()).getDisplayName()。
packagecom.bijian.study.listener;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.ApplicationListener;importorg.springframework.context.event.ContextRefreshedEvent;importorg.springframework.web.context.support.XmlWebApplicationContext;importcom.bijian.study.monitor.FileMonitor;public class ContextFileListener implements ApplicationListener{
@AutowiredprivateFileMonitor fileMonitor;private static boolean isStart = false;
@Overridepublic voidonApplicationEvent(ContextRefreshedEvent event) {if(event.getSource() instanceofXmlWebApplicationContext){
System.out.println(((XmlWebApplicationContext)event.getSource()).getDisplayName());if(((XmlWebApplicationContext)event.getSource()).getDisplayName().equals("Root WebApplicationContext")){try{
fileMonitor.monitor();
fileMonitor.start();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
System.out.println(((XmlWebApplicationContext)event.getSource()).getDisplayName());输出的内容如下所示:
Root WebApplicationContext
WebApplicationContextfor namespace 'springMVC-servlet'WebApplicationContextfor namespace 'springMVC-servlet'
2.使用一个类变量isStart,控制只执行一次
packagecom.bijian.study.listener;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.ApplicationListener;
importorg.springframework.context.event.ContextRefreshedEvent;
importorg.springframework.web.context.support.XmlWebApplicationContext;
importcom.bijian.study.monitor.FileMonitor;
public class ContextFileListener implements ApplicationListener{
@Autowired
privateFileMonitor fileMonitor;
private static boolean isStart = false;
@Override
public voidonApplicationEvent(ContextRefreshedEvent event) {
if (!isStart) {
isStart = true;
try{
fileMonitor.monitor();
fileMonitor.start();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}