配置管理库 typesafe.config 的简单使用

SimpleLibContext.java

package config_lib;

/**
 * Author:  HeatDeath
 * Date:    2018/2/23
 * Desc:
 */

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

// Whenever you write a library, allow people to supply a Config but
// also default to ConfigFactory.load if they don't supply one.
// Libraries generally have some kind of Context or other object
// where it's convenient to place the configuration.

public class SimpleLibContext {
    private Config config;

    // we have a constructor allowing the app to provide a custom Config
    public SimpleLibContext(Config config) {
        this.config = config;
        // This verifies that the Config is sane and has our
        // reference config. Importantly, we specify the "simple-lib"
        // path so we only validate settings that belong to this
        // library. Otherwise, we might throw mistaken errors about
        // settings we know nothing about.
        config.checkValid(ConfigFactory.defaultReference(), "simple-lib");
    }

    // This uses the standard default Config, if none is provided,
    // which simplifies apps willing to use the defaults
    public SimpleLibContext() {
        this(ConfigFactory.load());
    }

    // this is the amazing functionality provided by simple-lib
    public void printSetting(String path) {
        System.out.println("The setting '" + path + "' is: " + config.getString(path));
    }
}

ConfigLibUse.java

package config_lib;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

/**
 * Author:  HeatDeath
 * Date:    2018/2/23
 * Desc:
 */
public class ConfigLibUse {
    public static void main(String[] args) {
        // example of how system properties override; note this
        // must be set before the config lib is used
        System.setProperty("simple-lib.whatever", "This value comes from a system property");

        // Load our own config values from the default location,
        // application.conf、application.properties、application.conf、reference.conf
        Config conf = ConfigFactory.load();
        System.out.println("The answer is: " + conf.getString("simple-app.answer"));
        System.out.println(conf.getString("log4jaaa.bbb.ccc"));

        Config conf_1 = ConfigFactory.load("log4j.properties");
        System.out.println(conf_1.getString("log4j.rootLogger"));

        Config conf_2 = ConfigFactory.load("config/context.properties");
        System.out.println(conf_2.getString("httpclient.agent"));
        System.out.println(conf_2.getString("sso.auth.appkey"));


        // In this simple app, we're allowing SimpleLibContext() to
        // use the default config in application.conf ; this is exactly
        // the same as passing in ConfigFactory.load() here, so we could
        // also write "new SimpleLibContext(conf)" and it would be the same.
        // (simple-lib is a library in this same examples/ directory).
        // The point is that SimpleLibContext defaults to ConfigFactory.load()
        // but also allows us to pass in our own Config.
        SimpleLibContext context = new SimpleLibContext();
        context.printSetting("simple-lib.foo");
        context.printSetting("simple-lib.hello");
        context.printSetting("simple-lib.whatever");

    }
}

输出结果

The answer is: 42
'.'yyyy-MM-dd
INFO,console,stdout
AuthSSoDemo-Agent
${app.key}
The setting 'simple-lib.foo' is: This value comes from simple-app's application.conf
The setting 'simple-lib.hello' is: This value comes from simple-lib's reference.conf
The setting 'simple-lib.whatever' is: This value comes from a system property

Process finished with exit code 0

项目的目录结构

配置管理库 typesafe.config 的简单使用_第1张图片


Maven 依赖

        
        <dependency>
            <groupId>com.typesafegroupId>
            <artifactId>configartifactId>
            <version>1.3.3version>
        dependency>

说明:

如果 classpath 下找不到应该有的配置文件,使用 Maven 的 clean 和 install 命令搞一下即可

配置管理库 typesafe.config 的简单使用_第2张图片


参考资料

1、https://github.com/lightbend/config/blob/master/examples/java/simple-lib/src/main/java/simplelib/SimpleLibContext.java

2、https://github.com/lightbend/config/blob/master/README.md#api-example

3、Java基础学习总结(66)——配置管理库typesafe.config教程
http://blog.csdn.net/u012562943/article/details/52788494


你可能感兴趣的:(Java基础)