之前提到过在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));
}
}