spring笔记-PropertySource
2018-06-10 本文已影响12人
兴浩
抽象了对数据源键值对的操作
public class PropertySourceTest {
@Test
public void test1() throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("encoding", "gbk");
PropertySource propertySource1 = new MapPropertySource("map", map);
System.out.println(propertySource1.getProperty("encoding"));
}
@Test
public void test2() throws IOException {
Properties props=new Properties();
props.setProperty("encoding", "gbk");
PropertySource propertySource2 = new PropertiesPropertySource("resource", props); //name, location
System.out.println(propertySource2.getProperty("encoding"));
}
@Test
public void test3() throws IOException {
ResourcePropertySource propertySource2 = new ResourcePropertySource("resource", "classpath:core/env/resources.properties"); //name, location
System.out.println(propertySource2.getProperty("encoding"));
}
@Test
public void test4() throws IOException {
PropertySource propertySource = new SystemEnvironmentPropertySource("resource", (Map)System.getenv()); //name, location
System.out.println(propertySource.getProperty("USER"));
}
@Test
public void test5() throws IOException {
CompositePropertySource compositePropertySource = new CompositePropertySource("composite");
compositePropertySource.addPropertySource(new ResourcePropertySource("resource", "classpath:core/env/resources.properties"));
compositePropertySource.addPropertySource(new SystemEnvironmentPropertySource("resource", (Map)System.getenv()));
System.out.println(compositePropertySource.getProperty("encoding"));
System.out.println(compositePropertySource.getProperty("USER"));
}
@Test
public void test6() throws IOException {
//省略propertySource1/propertySource2
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new ResourcePropertySource("resource", "classpath:core/env/resources.properties"));
propertySources.addLast(new SystemEnvironmentPropertySource("env", (Map)System.getenv()));
System.out.println(propertySources.get("resource").getProperty("encoding"));
System.out.println(propertySources.get("env").getProperty("USER"));
}
}