JAVA 配置管理库 typesafe.config
来源:互联网 发布:淘宝网中老年鞋 编辑:程序博客网 时间:2024/06/05 02:04
Typesafe的Config库,纯Java写成、零外部依赖、代码精简、功能灵活、API友好。支持Java properties、JSON、JSON超集格式HOCON以及环境变量。它也是Akka的配置管理库。
特性:
- 纯java实现,无任何依赖
- 充分的测试
- 支持: Java properties, JSON
- 可以合并各种格式的配置文件
- 可以通过文件、urls、classpath加载配置
- 支持多层嵌套的配置方式
- 识别Java system properties, 如java -Dmyapp.foo.bar=10
- 可以转换长短,大小等单位。如配置文件中timeout=10s,则可以转换成任意的毫秒或者类型转换,比如yes可以转换为boolean类型的true:
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)
目前config只支持配置文件,如果想从数据库获取配置文件,需要自己定义。 config库很擅长合并配置。
Example
默认加载classpath下的application.conf,application.json和application.properties文件。通过ConfigFactory.load()加载。
application.conf:
conf.foo="This value comes from simple-app's application.conf"conf.whatever = "This value comes from simple-app's application.conf"
application.properties:
properties.foo="This value comes from simple-app's application.properties"properties.whatever = "This value comes from simple-app's application.properties"
application.json:
{ "id":1, "label":"person", "outE":{ "uses":[ { "id":16, "inV":11, "properties":{ "skill":5 } }, { "id":15, "inV":10, "properties":{ "skill":4 } } ], "develops":[ { "id":13, "inV":10, "properties":{ "since":2009 } }, { "id":14, "inV":11, "properties":{ "since":2010 } } ] }, "properties":{ "name":[ { "id":0, "value":"marko" } ], "location":[ { "id":6, "value":"san diego", "properties":{ "startTime":1997, "endTime":2001 } }, { "id":7, "value":"santa cruz", "properties":{ "startTime":2001, "endTime":2004 } }, { "id":8, "value":"brussels", "properties":{ "startTime":2004, "endTime":2005 } }, { "id":9, "value":"santa fe", "properties":{ "startTime":2005 } } ] }}
public class SimpleTypesafeConfig { private Config config; public SimpleTypesafeConfig(Config config) { this.config = config; config.checkValid(ConfigFactory.defaultReference(), "conf"); } public SimpleTypesafeConfig() { this(ConfigFactory.load()); } public void printSetting(String path) { System.out.println("The setting '" + path + "' is: " + config.getString(path)); } public static void main(String[] args) { SimpleTypesafeConfig s = new SimpleTypesafeConfig(); s.printSetting("properties.whatever");//application.properties //output: The setting 'properties.whatever' is: "This value comes from simple-app's application.properties" s.printSetting("conf.whatever");//application.conf //output:The setting 'conf.whatever' is: This value comes from simple-app's application.conf s.printSetting("label");//application.conf //output: person }}
其中核心加载代码:
config.checkValid(ConfigFactory.defaultReference(), "conf");
底层调用:
private static ClassLoader checkedContextClassLoader(String methodName) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if(loader == null) { throw new BugOrBroken("Context class loader is not set for the current thread; if Thread.currentThread().getContextClassLoader() returns null, you must pass a ClassLoader explicitly to ConfigFactory." + methodName); } else { return loader; } }
阅读全文
0 0
- JAVA 配置管理库 typesafe.config
- 试用配置管理库typesafe.config
- Java基础学习总结(66)——配置管理库typesafe.config教程
- TypeSafe config & HOCON + Read properties in java (with example)
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- com.typesafe.config加载任意位置的配置文件
- com/typesafe/config/ConfigException : Unsupported major.minor version
- 通过Typesafe Activator创建akka java sample
- Java Tip 27: Typesafe constants in C++ and Java
- 译 Programming with typesafe enums and annotations in Java 5
- 多重环境下web.config配置管理解决方案
- ASP.NET的配置管理---machine.config
- ASP.NET的配置管理---web.config
- python flask中config配置管理问题
- typesafe activator
- CI框架源码阅读笔记7 配置管理组件 Config.php
- CI框架源码阅读笔记7 配置管理组件 Config.php
- Linux常用目录的作用及文件搜索命令
- Unity_简单对象池_032
- 使用Maven构建多模块层级项目
- 1146: 吃糖果
- androidstudio快捷键
- JAVA 配置管理库 typesafe.config
- 怎么样简单的进行dwg转pdf格式
- 学会自己编写AndroidStduio插件
- tomcat 8.5.8 Bug 60372
- Android ANR时间
- c++ string类构造器重写和运算符重载代码
- Battery-Historian基本参数介绍
- sql查询优化
- ADF 弹出框