Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

5.4. 手工载入 *.properties 文件

5.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();
		}
	}	
			
			

5.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 "";
    }