您的位置:首页 > 其它

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

2017-07-11 09:15 393 查看
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 {

@Test
public 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();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: