JAVA 配置管理 读取properties文件

import java.io.InputStream;
import java.util.Properties;
public class ConfigurationManager {
    //Properties对象采用private修饰, 表示是其私有,外界不可改变
    private static Properties prop=new Properties();
    static {
        try{
            InputStream in=ConfigurationManager.class
                    .getClassLoader().getResourceAsStream("my.properties");
            prop.load(in);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static String getProperty(String key){
        return prop.getProperty(key);
    }
}

然后在 resources 里面放入你的配置文件 ,  以 key value来读取.

 

 在外部测试 , 先导入这个静态类, 你就直接调用 getproperty , 就会返回 value 了

猜你喜欢

转载自www.cnblogs.com/alpha-cat/p/13205645.html