typesafe读取配置文件

typesafe 可以读取properties文件、.config 文件

1. pom

        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.4.0</version>
        </dependency>

2. 测试

1. 读取properties

public static void main(String[] args) throws InterruptedException, ExecutionException {
        String resourceBasename = "/Users/qiao-zhi/app/ideaspace/vue-boot-chart/chart-server/src/main/resources/application.properties";
        Config load = ConfigFactory.parseFile(new File(resourceBasename));
        System.out.println(load.getString("logging.level.root"));
        System.out.println("===");
        load.entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        });
    }

结果:

INFO
===
mybatis-plus.mapper-locations    Quoted("classpath:mapper/**/*Mapper.xml")
spring.redis.pool.max-idle    Quoted("10")
spring.jackson.serialization.write-dates-as-timestamps    Quoted("false")
...

2. 读取.config 文件

test.config 文件

username=zs
fullname = '李四 是啥米'
age = 25
address= [
{"name": "北京"},
{"name": "西城"}
]

like=["篮球", "羽毛球", "pingpa球"]

 测试代码

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        String resourceBasename = "/Users/qiao-zhi/app/ideaspace/vue-boot-chart/chart-server/src/main/resources/test.config";
        Config load = ConfigFactory.parseFile(new File(resourceBasename));
        System.out.println(load.getString("fullname"));
        System.out.println("===");
        System.out.println(load.getStringList("like"));
        System.out.println("===");
        load.entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        });
    }

结果:

'李四 是啥米'
===
[篮球, 羽毛球, pingpa球]
===
fullname    Quoted("'李四 是啥米'")
username    Unquoted("zs")
age    ConfigInt(25)
like    SimpleConfigList(["篮球","羽毛球","pingpa球"])
address    SimpleConfigList([{"name":"北京"},{"name":"西城"}])

 

posted @ 2022-12-02 23:20  QiaoZhi  阅读(354)  评论(0编辑  收藏  举报