美文网首页我爱编程
spring笔记-PropertySource

spring笔记-PropertySource

作者: 兴浩 | 来源:发表于2018-06-10 23:15 被阅读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"));
    }

}

相关文章

网友评论

    本文标题:spring笔记-PropertySource

    本文链接:https://www.haomeiwen.com/subject/mjaqeftx.html