读取配置文件包含properties和xml文件

读取properties配置文件

/**
 * 读取配置文件
 * @author ll-t150
 */
public class Utils {
    
    private static Properties props = new Properties();
    private static InputStream inputStream = null;
    private static String confPath = null;
    
    static {
        confPath = System.getProperty("conf");
        try {
            if(!StringUtils.isEmpty(confPath)){
                inputStream = new FileInputStream(confPath.concat("/dinpay-sync.properties"));
            }else{
                ClassLoader cl = Utils.class.getClassLoader();
                inputStream = cl.getResourceAsStream("dinpay-sync.properties");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            props.load(inputStream);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    
    public static String getString(String key){
        String val = null;
        val = props.getProperty(key);
        return val;
    }

读取spring的xml文件

 private static AbstractApplicationContext appContext = null;
    private static final String XML_EXPRESSION = "classpath*:applicationContext*.xml";

    static {
        // 后续来读取命令中的conf 例如 java -Dconf=conf/*.xml -classpath .:lib/*
        if (System.getProperty("conf") != null) {
            appContext = new FileSystemXmlApplicationContext(System.getProperty("conf").concat("/applicationContext-sync.xml"));
        } else {
            appContext = new ClassPathXmlApplicationContext(XML_EXPRESSION);
        }
    }

 appContext.registerShutdownHook();
 appContext.start();
原文地址:https://www.cnblogs.com/atomicbomb/p/6755776.html