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; 
 | 
  
 | 
  
 | 
/** 
 | 
 * 加载config.xml配置的工具类 
 | 
 * 此类没有注解 @Component 所以在集成其的子模块中加入此监听器 
 | 
 * 在springboot中一般不用config.xml配置 
 | 
 */ 
 | 
@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; 
 | 
        } 
 | 
    } 
 | 
} 
 |