30.4.1. 家在 resources 目录中的指定文件
@RequestMapping("/config")
@ResponseBody
public void config() {
try {
Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("/config.properties"));
for(String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
System.out.println(key + " => " + value);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
30.4.2. 从 InputStreamReader 打开 application.properties
@GetMapping("/properties")
public String properties() {
Properties properties = new Properties();
try {
InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("application.properties")), StandardCharsets.UTF_8);
properties.load(inputStreamReader);
return properties.getProperty("app.id");
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}