欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java如何实现项目启动时执行指定方法

时间:2021-07-21 08:22:49|栏目:JAVA代码|点击:

本文实例为大家分享了java项目启动时执行指定方法,供大家参考,具体内容如下

想到的就是监听步骤如下:

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();
 } 
}

上一篇:java 画pdf用itext调整表格宽度、自定义各个列宽的方法

栏    目:JAVA代码

下一篇:Java俄罗斯方块小游戏

本文标题:java如何实现项目启动时执行指定方法

本文地址:http://www.codeinn.net/misctech/159913.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有