liurunyu
2023-11-04 df37487916ebc1ff8d8e0f8b088c5b6af1e582ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.dy.common.webListener;
 
import com.dy.common.util.Config;
import com.dy.common.util.ConfigProperties;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import lombok.extern.slf4j.Slf4j;
 
import java.io.InputStream;
import java.net.URL;
 
@Slf4j
public class ConfigListener implements ServletContextListener {
 
    //
    public static final String CONFIGFILENAMES = "configFileNames" ;
 
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContextListener.super.contextInitialized(event);
        ServletContext con = event.getServletContext();
        this.init(con);
    }
 
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        ServletContextListener.super.contextDestroyed(event);
    }
 
 
    /**
     * 解析系统配置
     * @return
     */
    private boolean init(ServletContext con){
        try {
            InputStream in = this.getClass().getResourceAsStream("/config/config.properties") ;
            if(in == null){
                throw new Exception("未找到配置/config/config.properties属性配置文件名称!") ;
            }
            ConfigProperties.init(in, false);
 
            String configFileName = con.getInitParameter(CONFIGFILENAMES);
            if(configFileName == null || configFileName.trim().equals("")){
                throw new Exception("未配置config类文件名称!") ;
            }
            String confs[] = configFileName.split(",");
            String conf = null ;
            for(int i = 0 ; i < confs.length ; i++){
                conf = confs[i].trim() ;
                if(conf != null && !conf.equals("")){
                    URL configFileURL = null;
                    configFileURL = ConfigListener.class.getResource("/config/" + conf);
                    if(configFileURL == null){
                        configFileURL = ConfigListener.class.getResource("/" + conf);
                    }
                    Config.getInstance().init(configFileURL) ;
                }
            }
            return true;
        } catch (Exception e) {
            System.out.println("系统启动时,初始化配置出错 !");
            System.out.println(e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}