scala 中配置文件之 config
来源:互联网 发布:申请淘宝介入流程 编辑:程序博客网 时间:2024/05/17 08:52
Typesafe的Config库,纯Java写成、零外部依赖、代码精简、功能灵活、API友好。支持Java properties、JSON、JSON超集格式HOCON以及环境变量。它也是Akka的配置管理库.
Overview
纯java实现,无任何依赖充分的测试支持: Java properties, JSON, and a human-friendly JSON superset可以合并各种格式的配置文件可以通过文件、urls、classpath加载配置支持多层嵌套的配置方式识别Java system properties, 如java -Dmyapp.foo.bar=10可以转换长短,大小等单位。如配置文件中timeout=10s,则可以转换成任意的毫秒或者类型转换,比如yes可以转换为boolean类型的trueJSON superset features: comments includes substitutions ("foo" : ${bar}, "foo" : Hello ${who}) properties-like notation (a.b=c) less noisy, more lenient syntax substitute environment variables (logdir=${HOME}/logs)
example
默认加载classpath下的application.conf,application.json和application.properties文件。通过ConfigFactory.load()加载。
# these are our own config values defined by the appsimple-app { answer=42}
# Here we override some values used by a librarysimple-lib.foo="This value comes from simple-app's application.conf"simple-lib.whatever = "This value comes from simple-app's application.conf"
public class SimpleLibContext { private Config config; //指定配置文件 public SimpleLibContext(Config config) { this.config = config; config.checkValid(ConfigFactory.defaultReference(), "simple-lib"); } // 默认加载classpath下的application.* public SimpleLibContext() { this(ConfigFactory.load()); } //打印 public void printSetting(String path) { System.out.println("The setting '" + path + "' is: " + config.getString(path)); } public static void main(String[] args) { SimpleLibContext s = new SimpleLibContext(); s.printSetting("simple-app.answer"); }}
config 文件的操作
配置内容即可以是层级关系,也可以用”.”号分隔写成一行
如:
akka { host = "0.0.0.0" port = 9999}
用“.” 写成一行,如:
akka.host = "0.0.0.0"akka.port = 9999
当有多个配置文件时,可以使用include 引入,如
calculator { include "common" akka { remote.netty.port = 2552 }}
另外:
akka2使用Typesafe Config库,可以使用ConfigFactory.load()加载配置文件,默认加载classpath下的application.conf, application.json and application.properties文件。ActorSystem将会把这些配置和reference.conf合并(merge)起来。
如果要写akka应用,将配置写在classpath根目录下的application.conf文件中。
如果要写基于akka的lib包,将配置写在jar包内的根目录下的reference.conf文件中.
如果多个config 文件有冲突时,解决方案有
1. a.withFallback(b)? //a和b合并,如果有相同的key,以a为准
2. a.withOnlyPath(String path)? //只取a里的path下的配置
3. a.withoutPath(String path) //只取a里出path外的配置
大家可以参考一下,conifg 的api 描述
/** * Obtains the default reference configuration, which is currently created * by merging all resources "reference.conf" found on the classpath and * overriding the result with system properties. The returned reference * configuration will already have substitutions resolved. * * <p> * Libraries and frameworks should ship with a "reference.conf" in their * jar. * * <p> * The reference config must be looked up in the class loader that contains * the libraries that you want to use with this config, so the * "reference.conf" for each library can be found. Use * {@link #defaultReference(ClassLoader)} if the context class loader is not * suitable. * * <p> * The {@link #load()} methods merge this configuration for you * automatically. * * <p> * Future versions may look for reference configuration in more places. It * is not guaranteed that this method <em>only</em> looks at * "reference.conf". * * @return the default reference config for context class loader */ public static Config defaultReference() { return defaultReference(checkedContextClassLoader("defaultReference")); }
- scala 中配置文件之 config
- mybatis中config配置文件
- CKEditor之配置文件config.js
- WPF中读写config配置文件
- Discuz!NT之配置文件类[discuz.config]
- Discuz!NT之配置文件类[discuz.config]
- Discuz!NT之配置文件类[discuz.config]
- Discuz!NT之配置文件类[discuz.config]
- firefly笔记之配置文件config.json
- Flask 配置文件config之from_object 篇
- Asp.Net 之 Web.config 配置文件详解
- asp.net中Web.Config配置文件详解
- c#,WinForm中读写配置文件App.config
- asp.net中Web.Config配置文件详解
- Ajax中web.config的配置文件
- 三层架构中web.config配置文件使用
- 在WPF中读写config配置文件
- C#中动态读写App.config配置文件
- 九度oj 1451
- cocos2dx-屏幕设置
- JavaScript
- 南邮 OJ 2070 马尔扎哈的疑惑
- 3126POJ
- scala 中配置文件之 config
- Tcl学习之--流程控制|过程
- app后端设计--短信,邮件,推送服务
- Android View触摸事件传递机制
- 南邮 OJ 2071 大发明家的童年
- uva 10003 动态规划
- sql server 2008出现远程过程调用失败
- iOS 7:漫谈集合视图(CollectionView)
- UIAlertView 实现自动消失