利用java怎么在項目啟動時執(zhí)行指定的方法-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)利用java怎么在項目啟動時執(zhí)行指定的方法,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比金林網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式金林網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋金林地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。

1.配置web.xml

 <listener> 
 <listener-class>com.listener.InitListener</listener-class> 
</listener>

2.編寫InitListener類

package com.listener;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.seegot.util.PropertyUtil;

public class InitListener implements ServletContextListener {

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
  System.out.println("================>[ServletContextListener]自動加載啟動開始."); 
  String resourceFilesPath = PropertyUtil.getProperty("tempZipPath");
  clearFiles(resourceFilesPath);
 }
 // 刪除文件和目錄
 private static boolean clearFiles(String workspaceRootPath) {
  File file = new File(workspaceRootPath);
  if (file.exists()) {
   deleteFile(file);
  }
  // resources 文件夾被刪除后需新建
  if (!file.exists() && workspaceRootPath.endsWith("resources")) {
   return file.mkdir();
  } else if (!file.exists()) {
   return true;
  }
   return false;
  }

 private static boolean deleteFile(File file) {
  if (file.isDirectory()) {
   File[] files = file.listFiles();
   for (int i = 0; i < files.length; i++) {
    deleteFile(files[i]);
   }
  }
   return file.delete();
 } 
}

關(guān)于利用java怎么在項目啟動時執(zhí)行指定的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:利用java怎么在項目啟動時執(zhí)行指定的方法-創(chuàng)新互聯(lián)
路徑分享:http://www.muchs.cn/article36/pessg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、手機網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、標(biāo)簽優(yōu)化軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)