java 監聽窗口是否改變_JAVA項目監聽文件是否發生變化

一.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();

}

}

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/533965.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/533965.shtml
英文地址,請注明出處:http://en.pswp.cn/news/533965.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

笨辦法學python3 pdf 腳本之家_解決python3輸入的坑——input()

如下所示:a,b,c,d input()很簡單的代碼,如果輸入為1 -1 -2 3結果會報錯,原因在于input函數會將你的輸入作為python腳本運行,那么輸入就變成了1-1 -2 3,即0 -2 3結果當然是錯誤的了,解決辦法就是將輸入用引…

java 數組寫法_java書寫、數據類型、數組定義

這里只記錄java與php、javascript不同的地方,相同的地方就不贅述了。1.java文件源碼為以.java為后綴的文件,字節碼文件是以.class為后綴的文件。2.寫好一個java源碼之后,cmd進入源碼文件盤符,用命令 javac helloworld.java將源碼轉…

python爬蟲高級知識點_Python爬蟲知識點梳理總結,殿堂級小白入門必讀

數據分析是任何技術一樣。你應該學習的目標。目標就像燈塔,指引你前進。我見過很多合作伙伴學習學習,然后學會放棄。事實上,很大一部分原因是沒有明確的目標,所以你必須清楚學習的目的。你準備學習爬行之前,問問你自己為什么你想學爬行。有些人為了工作,一些為了好玩,和做一定黑…

java running_Running

/****/package test;import java.sql.ResultSet;import java.sql.SQLException;/*** author huangqin**/public class QuestString {private int curPage;//當前頁數private int maxPage;//最大頁數private int maxRowCount;//總記錄數private int pageSize2;//每頁顯示的記錄數…

python停用詞表_多版本中文停用詞詞表 + 多版本英文停用詞詞表 + python詞表合并程序...

文章簡介與更新記錄如果你只想獲取中文停用詞此表,請直接到文章結尾下載項目文件,其中包括三個中文停用詞詞表,一個英文停用詞詞表和一個合并詞表的.py文件2017/07/04 創建文章,上傳文件2017/07/04 更新了合并代碼,添加了新的中文停用詞表(哈工大擴展版本)和一個新的停用詞表,現…

mysql collect_set_hive列轉行 (collect_set())

一、問題hive如何將a b 1a b 2a b 3c d 4c d 5c d 6變為:a b 1,2,3c d 4,5,6二、數據test.txta b 1a b 2a b 3c d …

python編寫遞歸函數和非遞歸函數、輸出斐波那契數列_分別用非遞歸和遞歸的方法編寫函數求斐波那契數列第n項。斐波那契數列1,1,2,3,5,8,13,…...

展開全部/**已知Fibonacci數列&#xff1a;1,1,2,3,5,8,……&#xff0c;F(1)1&#xff0c;F(2)1&#xff0c;F(n)F(n-1)F(n-2)*/#include #include typedef long long int int64;//方法1&#xff0c;遞歸法int64 Fibonacci(int n){int64 sum;if(n<0){printf("參數值e6…

python3.6安裝ipython_centos6.5下安裝python3.6、pip、ipython

一.先更換系統源為了下載順暢一般都會更改為國內源。1 cd /etc/yum.repos.d/2 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo #下載網易源3 mv CentOS-Base.repo CentOS-Base.repo.ori #備份源4 mv CentOS6-Base-163.repo CentOS-Base.repo #把網易源更改為默認源二…

java 多線程的同步問題_java多線程解決同步問題的幾種方式,原理和代碼

wait()/notify()方法await()/signal()方法BlockingQueue阻塞隊列方法PipedInputStream/PipedOutputStream阻塞隊列的一個簡單實現&#xff1a;public class BlockingQueue {private List queue new LinkedList();private int limit 10;public BlockingQueue(int limit){this…

python期末大作業_大一期末考試很重要,考得好不僅有機會有錢拿,還有機會換專業...

現階段很多高校放寒假的時間已經公布&#xff0c;這也就意味著&#xff0c;大學期末考試即將到來。對于大一新生來說&#xff0c;大學的期末考試是比較新鮮的&#xff0c;因為大家都沒有經歷過。經歷過大學考試的學生&#xff0c;都知道大學的大概學習模式&#xff0c;一般情況…

java http 302重定向_Java 純HTTP請求 禁止302自動重定向

Java 純HTTP Get請求獲取響應內容&#xff0c;如果發生302重定向&#xff0c;繼而模擬請求域獲取重定向后的響應內容。關鍵點&#xff1a;設置conn.setInstanceFollowRedirects為false即可示例代碼public static void main(String[] args) {try {StringBuffer buffer new Stri…

python 且_Pyface庫:一個基于pyqt、pyside、wx且簡化的python的GUI

1 說明&#xff1a;1.1 Pyface庫由大名鼎鼎的enthought出品。1.2 介紹&#xff1a;1.2.1 英文&#xff1a;traits-capable windowing framework.The pyface project contains a toolkit-independent GUI abstraction layer, which is used to support the "visualization&…

java方法的參數類型_Java 基礎 14 方法的重載 與 方法參數類型詳解

1.1 方法重載的概述和特點方法重載概述在同一個類中&#xff0c;允許存在一個以上的同名方法&#xff0c;只要它們的參數個數或者參數類型不同即可。方法重載特點與返回值類型無關&#xff0c;只看方法名和參數列表在調用時&#xff0c;虛擬機通過參數列表的不同來區分同名方法…

crv儀表上的i是什么指示燈_汽車打不著火是怎么回事,儀表盤汽車發動機故障燈亮是什么情況故障指示燈圖解大全集...

如果打不著火&#xff0c;那有可能是起動機壞了&#xff0c;有可能是電池沒電了&#xff0c;有可能是電路出現了問題&#xff0c;還有可能是點火系統出現了問題。汽車發動機的點火系統主要部件是火花塞和點火線圈&#xff0c;火花塞是一個需要定期更換的易損件。如果火花塞長時…

python極簡教程_Python 極簡教程(六)運算符

運算符&#xff0c;我們日常生活中使用的加減乘除&#xff0c;都是運算符的一種。當然這種一般我們稱為算術運算符&#xff0c;用于處理數字運算的。但是在計算機語言中&#xff0c;還有很多的運算符。用于處理不用的情況。主要有以下幾類&#xff1a;算術運算符比較運算符邏輯…

python函數可變長參數_day14 Python函數之可變長參數

函數參數1.形參變量只有在被調用時才分配內存單元&#xff0c;在調用結束時&#xff0c;即刻釋放所分配的內存單元。因此&#xff0c;形參只在函數內部有效。函數調用結束返回主調用函數后則不能再使用該形參變量2.實參可以是常量、變量、表達式、函數等&#xff0c;無論實參是…

ubuntu 安裝java jdk_「ubuntu安裝jdk」Ubuntu安裝jdk8的兩種方式 - seo實驗室

ubuntu安裝jdk安裝方式&#xff1a;1)&#xff1a;通過ppa(源) 方式安裝.2)&#xff1a;通過官網安裝包安裝.JDK官網下載地址一&#xff1a;使用ppa(源)方式安裝&#xff1a;1)&#xff1a;添加ppa源sudo add-apt-repository ppa:webupd8team/javasudo apt-get update2)&#x…

restful風格_什么是RESTful風格的API設計?

隨著移動互聯網的興起&#xff0c;RESTful風格的API設計也隨之流行起來&#xff0c;但我們說了那么多RESTful設計&#xff0c;它到底是什么&#xff1f;本篇文章帶大家來了解一下它的真實面目。RESTful概念首先&#xff0c;我們需要明確的是RESTful&#xff0c;它是一個理念&am…

java jdbc 增刪改封裝_JAVA JDBC 常規增刪改查簡單封裝

JAVA JDBC 常規增刪改查簡單封裝,可滿足大多基本要求作用&#xff1a;1&#xff0c; 查詢列表是直接返回List對象&#xff0c;不必再遍歷&#xff1b;2&#xff0c; 單條查詢直接返回對象&#xff1b;3&#xff0c; 執行sql僅需一個方法搞定&#xff1b;package com.Main.Tools…

python wget安裝_Macbook系統環境安裝wget的2個方法 - 傳統包及Homebrew安裝

考慮到自身項目的拓展需要&#xff0c;朋友建議學習Python爬蟲這樣對于做大數據采集有較大的幫助&#xff0c;老蔣雖然每天也都接觸一些腳本和程序的修改&#xff0c;但是并沒有專業和系統的學習某一項編程。所以還是準備陸續的學習Python語言&#xff0c;無論有沒有基礎&#…