java启动时执行_java怎么实现项目启动时执行指定方法

本文实例为大家共享了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();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持乐购源码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值