在做项目和应用程序的时候,经常会将一些常量或者链接数据库、服务器的信息保存在properties文件中。以便这些信息发生改变的时候,执行修改properties文件,而不需要需该程序代码。
举例子:
data.properties
key=1234567890abcdef
bid=1004
bidForDaka=10081
public class DataPro {
// private static final Logger log = LoggerFactory.getLogger(Env.class);
public static String get(String key) {
return getProperties().getProperty(key);
}
public static Properties getProperties() {
try {
return (Properties) SpringUtil.getBean("env");
} catch (Exception e) {
Properties properties = new Properties();
try {
File file = new File(Env.class.getResource("/").getPath(), "data.properties");
//log.warn("use {}",file);
InputStream inputStream = new FileInputStream(file);
properties.load(inputStream);
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return properties;
}
}