本文实例为大家共享了java项目启动时执行指定方法,供大家参考,详细内容如下
想到的就是监听步骤如下:
1.配置web.xml
com.listener.InitListener
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();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持乐购源码。