Sprint读取配置文件内容格式的String

来源:互联网 发布:网络推广专员薪资制度 编辑:程序博客网 时间:2024/06/06 00:07
package unit.test;import java.io.IOException;import java.util.Properties;import org.junit.Test;import org.springframework.core.io.ByteArrayResource;import org.springframework.core.io.support.PropertiesLoaderUtils;public class StringPropertyLoadTest {@Testpublic void testLoad(){String protertryStr = "spring.redis.database=0\n"+"spring.redis.host=localhost\n"+"spring.redis.port=8080\n"+"spring.redis.password=\n"+"#this is comment! \n"+"spring.redis.timeout=0\n"+"spring.redis.pool.max-active=8 #abcd   \n"+"spring.redis.pool.max-wait=-1\n"+"spring.redis.pool.max-idle=8\n"+"spring.redis.pool.min-idle=0\n";ByteArrayResource resource = new ByteArrayResource(protertryStr.getBytes(), protertryStr);try {Properties properties  =PropertiesLoaderUtils.loadProperties(resource);properties.entrySet().forEach(p ->{System.out.println(p.getKey()+"="+p.getValue());});} catch (IOException e) {e.printStackTrace();}}}