Akka Config Lib的簡單使用java
akka框架中使用該庫配置akka,該項目地址爲https://github.com/typesafehub/config git
首先在classpath下定義文件爲:github
complex1.confjson
complex-app { something = "hello world" # here we want a simple-lib-context unique to our app # which can be custom-configured. In code, we have to # pull out this subtree and pass it to simple-lib. simple-lib-context = { simple-lib { foo = "hello world complex1 foo" whatever = "hello world complex1 whatever" } } }
使用以下方式解析這個文件,api
package com.usoft6; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import com.usoft3.SystemSettingDemo; /** * Created by liyanxin on 2015/1/13. */ public class TypesafeConfigDemo { public static void main(String args[]) { // "config1" is just an example of using a file other than // application.conf // config1是在classpath路徑下的配置文件名,該文件類型是conf文件類型 Config config1 = ConfigFactory.load("complex1"); // use the config ourselves System.out.println(config1.getString("complex-app.something")); System.out.println(config1.getString("complex-app.simple-lib-context.simple-lib.foo")); System.out.println(config1.getString("complex-app.simple-lib-context.simple-lib.whatever")); // ConfigFactory.parseString使用parseString直接解析 Config config2 = ConfigFactory.parseString("akka.loggers = \"akka.testkit.TestEventListener\""); System.out.println(config2.getString("akka.loggers")); // 使用parseString 直接解析json字符串 Config config3 = ConfigFactory.parseString("{\"a\":\"b\", \"c\":\"d\"}"); System.out.println(config3.getString("a")); System.out.println(config3.getString("c")); } }
如上,還能夠在定義json字符串,直接用typesafe config lib 解析爲config對象。app
以下,在classpath下定義兩個配置文件,
框架
complex1.confthis
complex-app { something = "hello world" # here we want a simple-lib-context unique to our app # which can be custom-configured. In code, we have to # pull out this subtree and pass it to simple-lib. simple-lib-context = { simple-lib { foo = "hello world complex1 foo" whatever = "hello world complex1 whatever" } } }
complex2.confspa
complex-app { something = "hello world typesafe conf" } simple-lib.foo = "hello world foo" simple-lib.whatever = "hello world whatever"
如何合併這兩個配置文件,以下,code
package com.usoft6; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; /** * Created by liyanxin on 2015/1/13. */ public class WithFallBackDemo { public static void main(String args[]) { Config firstConfig = ConfigFactory.load("complex1.conf"); Config secondConfig = ConfigFactory.load("complex2.conf"); //a.withFallback(b) a和b合併,若是有相同的key,以a爲準 Config finalConfig = firstConfig.withFallback(secondConfig); System.out.println(finalConfig.getString("complex-app.something")); //hello world System.out.println("simple-lib.foo"); //這個配置項是在complex2.conf中的 } }
運行結果,
hello world
simple-lib.foo
同上定義的兩個conf配置文件,以下代碼示例,
package com.usoft6; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; /** * Created by liyanxin on 2015/1/13. */ public class WithOnlyPathDemo { public static void main(String args[]) { Config firstConfig = ConfigFactory.load("complex1.conf"); Config secondConfig = ConfigFactory.load("complex2.conf"); //a.withFallback(b) a和b合併,若是有相同的key,以a爲準 Config finalConfig = firstConfig.withOnlyPath("complex-app.simple-lib-context").withFallback(secondConfig); System.out.println(finalConfig.getString("complex-app.something")); //hello world System.out.println("simple-lib.foo"); //這個配置項是在complex2.conf中的 } }
運行結果,
hello world typesafe conf
simple-lib.foo
其餘api請參照文檔
==============END==============