spring笔记-PropertySource

抽象了对数据源键值对的操作

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"));
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容