zoukankan      html  css  js  c++  java
  • java项目启动时执行指定方法

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

    1.配置web.xml

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

    2.编写InitListener类

     1 package com.listener;
     2 
     3 import java.io.File;
     4 
     5 import javax.servlet.ServletContextEvent;
     6 import javax.servlet.ServletContextListener;
     7 
     8 import com.seegot.util.PropertyUtil;
     9 
    10 public class InitListener implements ServletContextListener {
    11 
    12     @Override
    13     public void contextDestroyed(ServletContextEvent arg0) {
    14         // TODO Auto-generated method stub
    15 
    16     }
    17 
    18     @Override
    19     public void contextInitialized(ServletContextEvent arg0) {
    20         // TODO Auto-generated method stub
    21         System.out.println("================>[ServletContextListener]自动加载启动开始."); 
    22         String resourceFilesPath = PropertyUtil.getProperty("tempZipPath");
    23         clearFiles(resourceFilesPath);
    24     }
    25     // 删除文件和目录
    26     private static boolean clearFiles(String workspaceRootPath) {
    27         File file = new File(workspaceRootPath);
    28         if (file.exists()) {
    29             deleteFile(file);
    30         }
    31         // resources 文件夹被删除后需新建
    32         if (!file.exists() && workspaceRootPath.endsWith("resources")) {
    33             return file.mkdir();
    34         } else if (!file.exists()) {
    35             return true;
    36         }
    37             return false;
    38         }
    39 
    40     private static boolean deleteFile(File file) {
    41         if (file.isDirectory()) {
    42             File[] files = file.listFiles();
    43             for (int i = 0; i < files.length; i++) {
    44                 deleteFile(files[i]);
    45             }
    46         }
    47             return file.delete();
    48     } 
    49 }
    View Code
  • 相关阅读:
    谈谈WPF中的CollectionView与CollectionViewSource (1)
    XPath语法备忘
    WPF中,如何将绑定源设置到单件实例
    避免让WPF资源字典变得杂乱臃肿
    自定义WPF面板
    [WPF疑难] 继承自定义窗口
    WPF高手:站出来,Show出来
    Windows Presentation Foundation Tools and Controls
    放送Ifttt邀请四枚
    使用Amazon S3 Service时报403错误的解决方法
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/7262825.html
Copyright © 2011-2022 走看看