Spring读取properties文件/Yaml文件(yml)

文章目录

  • 一、properties文件读取
  • 二、Yaml文件读取


一、properties文件读取

        ClassPathResource classPathResource = new ClassPathResource("application.properties");
        //判断文件是否存在
        boolean exists = classPathResource.exists();
        InputStream inputStream = classPathResource.getInputStream();

        Properties properties = new Properties();
        properties.load(inputStream);
        String search = properties.getProperty("search.elasticsearch.uris");
        String[] split = search.split(",");
        Arrays.stream(split).forEach(System.out::println);
        System.out.println(search);

        properties.clear();
        inputStream.close();

二、Yaml文件读取

        YamlPropertiesFactoryBean yamlProFb = new YamlPropertiesFactoryBean ();
        yamlProFb.setResources(new ClassPathResource("application2.yml"));
        Properties object = yamlProFb.getObject();
        String property = object.getProperty("search.elasticsearch.uris");

你可能感兴趣的:(java,springBoot,spring,java)