pom.xml使用多套环境及变量获取(2)

来源:互联网 发布:哈利波特怪味糖淘宝 编辑:程序博客网 时间:2024/06/15 20:13

之前提到过在spring配置文件中写bean的法子,其实不太方面,因为添加每个变量都需要在对应的bean中添加,可以有更加简单的法子,使用properties文件;
maven在打包时,会去替换${}这样的变量,剩下来的,便是如何去读取这个文件了,以下是代码:

public class Variables {    private static Properties properties = new Properties();    static {        InputStream in = Variables.class.getResourceAsStream("/variables.properties");        try {            properties.load(in);        } catch (Exception e) {            e.printStackTrace();        }    }    public static String get(String key) {        return String.valueOf(properties.get(key));    }}