package config
- Alphabetic
- Public
- All
Type Members
-
trait
Config extends ConfigMergeable
An immutable map from config paths to config values.
An immutable map from config paths to config values. Paths are dot-separated expressions such as
foo.bar.baz
. Values are as in JSON (booleans, strings, numbers, lists, or objects), represented byConfigValue
instances. Values accessed through theConfig
interface are never null.Config
is an immutable object and thus safe to use from multiple threads. There's never a need for "defensive copies."Fundamental operations on a
Config
include getting configuration values, resolving substitutions withConfig#resolve
, and merging configs usingConfig#withFallback(ConfigMergeable)
.All operations return a new immutable
Config
rather than modifying the original instance.Examples
You can find an example app and library <a href="https://github.com/lightbend/config/tree/master/examples">on GitHub. Also be sure to read the <a href="package-summary.html#package_description">package overview which describes the big picture as shown in those examples.
Paths, keys, and Config vs. ConfigObject
Config
is a view onto a tree ofConfigObject
; the corresponding object tree can be found throughConfig#root
.ConfigObject
is a map from config keys, rather than paths, to config values. Think ofConfigObject
as a JSON object andConfig
as a configuration API.The API tries to consistently use the terms "key" and "path." A key is a key in a JSON object; it's just a string that's the key in a map. A "path" is a parseable expression with a syntax and it refers to a series of keys. Path expressions are described in the <a href="https://github.com/lightbend/config/blob/master/HOCON.md">spec for Human-Optimized Config Object Notation. In brief, a path is period-separated so "a.b.c" looks for key c in object b in object a in the root object. Sometimes double quotes are needed around special characters in path expressions.
The API for a
Config
is in terms of path expressions, while the API for aConfigObject
is in terms of keys. Conceptually,Config
is a one-level map from paths to values, while aConfigObject
is a tree of nested maps from keys to values.Use
ConfigUtil#joinPath
andConfigUtil#splitPath
to convert between path expressions and individual path elements (keys).Another difference between
Config
andConfigObject
is that conceptually,ConfigValue
s with avalueType
ofNULL
exist in aConfigObject
, while aConfig
treats null values as if they were missing. (With the exception of two methods:Config#hasPathOrNull
andConfig#getIsNull
let you detectnull
values.)Getting configuration values
The "getters" on a
Config
all work in the same way. They never return null, nor do they return aConfigValue
withvalueType
ofNULL
. Instead, they throwConfigException.Missing
if the value is completely absent or set to null. If the value is set to null, a subtype ofConfigException.Missing
calledConfigException.Null
will be thrown.ConfigException.WrongType
will be thrown anytime you ask for a type and the value has an incompatible type. Reasonable type conversions are performed for you though.Iteration
If you want to iterate over the contents of a
Config
, you can get itsConfigObject
with#root
, and then iterate over theConfigObject
(which implementsjava.util.Map
). Or, you can use#entrySet
which recurses the object tree for you and builds up aSet
of all path-value pairs where the value is not null.Resolving substitutions
Substitutions are the
${foo.bar}
syntax in config files, described in the specification. Resolving substitutions replaces these references with real values.Before using a
Config
it's necessary to callConfig#resolve
to handle substitutions (thoughConfigFactory#load
and similar methods will do the resolve for you already).Merging
The full
Config
for your application can be constructed using the associative operationConfig#withFallback(ConfigMergeable)
. If you useConfigFactory#load
(recommended), it merges system properties over the top ofapplication.conf
over the top ofreference.conf
, usingwithFallback
. You can add in additional sources of configuration in the same way (usually, custom layers should go either just above or just belowapplication.conf
, keepingreference.conf
at the bottom and system properties at the top).Serialization
Convert a
Config
to a JSON or HOCON string by callingConfigObject#render
on the root object,myConfig.root.render
. There's also a variantConfigObject#render(ConfigRenderOptions)
which allows you to control the format of the rendered string. (SeeConfigRenderOptions
.) Note thatConfig
does not remember the formatting of the original file, so if you load, modify, and re-save a config file, it will be substantially reformatted.As an alternative to
ConfigObject#render
, thetoString
method produces a debug-output-oriented representation (which is not valid JSON).Java serialization is supported as well for
Config
and all subtypes ofConfigValue
.This is an interface but don't implement it yourself
Do not implement
Config
; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break. -
abstract
class
ConfigException extends RuntimeException with Serializable
All exceptions thrown by the library are subclasses of
ConfigException
.All exceptions thrown by the library are subclasses of
ConfigException
.- Annotations
- @SerialVersionUID()
- final class ConfigFactory extends AnyRef
-
trait
ConfigIncludeContext extends AnyRef
Context provided to a
ConfigIncluder
; this interface is only useful inside aConfigIncluder
implementation, and is not intended for apps to implement.Context provided to a
ConfigIncluder
; this interface is only useful inside aConfigIncluder
implementation, and is not intended for apps to implement.Do not implement this interface; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break.
-
trait
ConfigIncluder extends AnyRef
Implement this interface and provide an instance to
ConfigParseOptions.setIncluder()
to customize handling ofinclude
statements in config files.Implement this interface and provide an instance to
ConfigParseOptions.setIncluder()
to customize handling ofinclude
statements in config files. You may also want to implementConfigIncluderClasspath
,ConfigIncluderFile
, andConfigIncluderURL
, or not. -
trait
ConfigIncluderClasspath extends AnyRef
Implement this in addition to
ConfigIncluder
if you want to support inclusion of files with theinclude classpath("resource")
syntax.Implement this in addition to
ConfigIncluder
if you want to support inclusion of files with theinclude classpath("resource")
syntax. If you do not implement this but do implementConfigIncluder
, attempts to load classpath resources will use the default includer. -
trait
ConfigIncluderFile extends AnyRef
Implement this in addition to
ConfigIncluder
if you want to support inclusion of files with theinclude file("filename")
syntax.Implement this in addition to
ConfigIncluder
if you want to support inclusion of files with theinclude file("filename")
syntax. If you do not implement this but do implementConfigIncluder
, attempts to load files will use the default includer. -
trait
ConfigIncluderURL extends AnyRef
Implement this in addition to
ConfigIncluder
if you want to support inclusion of files with theinclude url("http://example.com")
syntax.Implement this in addition to
ConfigIncluder
if you want to support inclusion of files with theinclude url("http://example.com")
syntax. If you do not implement this but do implementConfigIncluder
, attempts to load URLs will use the default includer. -
trait
ConfigList extends List[ConfigValue] with ConfigValue
Subtype of
ConfigValue
representing a list value, as in JSON's[1,2,3]
syntax.Subtype of
ConfigValue
representing a list value, as in JSON's[1,2,3]
syntax.ConfigList
implementsjava.util.List
so you can use it like a regular Java list. Or call#unwrapped()
to unwrap the list elements into plain Java values.Like all
ConfigValue
subtypes,ConfigList
is immutable. This makes it threadsafe and you never have to create "defensive copies." The mutator methods fromjava.util.List
all throwjava.lang.UnsupportedOperationException
.The
ConfigValue#valueType
method on a list returnsConfigValueType#LIST
.Do not implement
ConfigList
; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break. -
trait
ConfigLoadingStrategy extends AnyRef
This method allows you to alter default config loading strategy for all the code which calls one of the methods, e.g.
This method allows you to alter default config loading strategy for all the code which calls one of the methods, e.g. ConfigFactory.load(String)
Usually you don't have to implement this interface but it may be required when you fixing a improperly implemented library with unavailable source code.
You have to define VM property
config.strategy
to replace default strategy with your own. - final class ConfigMemorySize extends AnyRef
-
trait
ConfigMergeable extends AnyRef
Marker for types whose instances can be merged, that is Config and ConfigValue.
Marker for types whose instances can be merged, that is Config and ConfigValue. Instances of
Config
andConfigValue
can be combined into a single new instance using the withFallback() method.Do not implement this interface; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break.
-
trait
ConfigObject extends ConfigValue with Map[String, ConfigValue]
Subtype of
ConfigValue
representing an object (AKA dictionary or map) value, as in JSON's curly brace{ "a" : 42 }
syntax.Subtype of
ConfigValue
representing an object (AKA dictionary or map) value, as in JSON's curly brace{ "a" : 42 }
syntax.An object may also be viewed as a
Config
by callingConfigObject#toConfig()
.ConfigObject
implementsjava.util.Map
so you can use it like a regular Java map. Or call#unwrapped()
to unwrap the map to a map with plain Java values rather thanConfigValue
.Like all
ConfigValue
subtypes,ConfigObject
is immutable. This makes it threadsafe and you never have to create "defensive copies." The mutator methods fromjava.util.Map
all throwjava.lang.UnsupportedOperationException
.The
ConfigValue#valueType
method on an object returnsConfigValueType#OBJECT
.In most cases you want to use the
Config
interface rather than this one. Call#toConfig()
to convert aConfigObject
to aConfig
.The API for a
ConfigObject
is in terms of keys, while the API for aConfig
is in terms of path expressions. Conceptually,ConfigObject
is a tree of maps from keys to values, while aConfig
is a one-level map from paths to values.Use
ConfigUtil#joinPath
andConfigUtil#splitPath
to convert between path expressions and individual path elements (keys).A
ConfigObject
may contain null values, which will haveConfigValue#valueType()
equal toConfigValueType#NULL
. IfConfigObject#get(Object)
returns Java's null then the key was not present in the parsed file (or wherever this value tree came from). Ifget("key")
returns aConfigValue
with typeConfigValueType#NULL
then the key was set to null explicitly in the config file.Do not implement interface
ConfigObject
; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break. -
trait
ConfigOrigin extends AnyRef
Represents the origin (such as filename and line number) of a
ConfigValue
for use in error messages.Represents the origin (such as filename and line number) of a
ConfigValue
for use in error messages. Obtain the origin of a value withConfigValue#origin
. Exceptions may have an origin, seeConfigException#origin
, but be careful becauseConfigException.origin()
may return null.It's best to use this interface only for debugging; its accuracy is "best effort" rather than guaranteed, and a potentially-noticeable amount of memory could probably be saved if origins were not kept around, so in the future there might be some option to discard origins.
Do not implement this interface; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break.
- final class ConfigOriginFactory extends AnyRef
- final class ConfigParseOptions extends AnyRef
-
trait
ConfigParseable extends AnyRef
An opaque handle to something that can be parsed, obtained from
ConfigIncludeContext
.An opaque handle to something that can be parsed, obtained from
ConfigIncludeContext
.Do not implement this interface; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break.
- final class ConfigRenderOptions extends AnyRef
- final class ConfigResolveOptions extends AnyRef
-
trait
ConfigResolver extends AnyRef
Implement this interface and provide an instance to
ConfigResolveOptions.appendResolver()
to provide custom behavior when unresolved substitutions are encountered during resolution.Implement this interface and provide an instance to
ConfigResolveOptions.appendResolver()
to provide custom behavior when unresolved substitutions are encountered during resolution.- Since
1.3.2
-
final
class
ConfigSyntax extends Enum[ConfigSyntax]
The syntax of a character stream (JSON, <a href="https://github.com/lightbend/config/blob/master/HOCON.md">HOCON aka ".conf", or Java properties).
- final class ConfigUtil extends AnyRef
-
trait
ConfigValue extends ConfigMergeable
An immutable value, following the JSON type schema.
An immutable value, following the JSON type schema.
Because this object is immutable, it is safe to use from multiple threads and there's no need for "defensive copies."
Do not implement interface
ConfigValue
; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break. - final class ConfigValueFactory extends AnyRef
-
final
class
ConfigValueType extends Enum[ConfigValueType]
The type of a configuration value (following the JSON type schema).
-
class
DefaultConfigLoadingStrategy extends ConfigLoadingStrategy
Default config loading strategy.
Default config loading strategy. Able to load resource, file or URL. Behavior may be altered by defining one of VM properties
config.resource
,config.file
orconfig.url
- class Optional extends Annotation with Annotation with ClassfileAnnotation
Value Members
-
object
ConfigBeanFactory
Factory for automatically creating a Java class from a Config.
Factory for automatically creating a Java class from a Config. See ConfigBeanFactory.create(Config,Class).
- Since
1.3.0
-
object
ConfigException extends Serializable
- Annotations
- @SerialVersionUID()
-
object
ConfigFactory
Contains static methods for creating Config instances.
Contains static methods for creating Config instances.
See also ConfigValueFactory which contains static methods for converting Java values into a ConfigObject. You can then convert a ConfigObject into a Config with ConfigObject#toConfig.
The static methods with "load" in the name do some sort of higher-level operation potentially parsing multiple resources and resolving substitutions, while the ones with "parse" in the name just create a ConfigValue from a resource and nothing else.
You can find an example app and library <a on GitHub. Also be sure to read the <a href="package-summary.html#package_description">package overview which describes the big picture as shown in those examples.
-
object
ConfigMemorySize
An immutable class representing an amount of memory.
An immutable class representing an amount of memory. Use static factory methods such as
ConfigMemorySize#ofBytes(long)
to create instances.- Since
1.3.0
-
object
ConfigOriginFactory
This class contains some static factory methods for building a ConfigOrigin.
This class contains some static factory methods for building a ConfigOrigin. ConfigOrigins are automatically created when you call other API methods to get a ConfigValue or Config. But you can also set the origin of an existing ConfigValue, using ConfigValue.withOrigin(ConfigOrigin).
- Since
1.3.0
-
object
ConfigParseOptions
A set of options related to parsing.
A set of options related to parsing.
This object is immutable, so the "setters" return a new object.
Here is an example of creating a custom
ConfigParseOptions
:ConfigParseOptions options = ConfigParseOptions.defaults() .setSyntax(ConfigSyntax.JSON) .setAllowMissing(false)
-
object
ConfigRenderOptions
A set of options related to rendering a
ConfigValue
.A set of options related to rendering a
ConfigValue
. Passed toConfigValue#render(ConfigRenderOptions)
.Here is an example of creating a
ConfigRenderOptions
:ConfigRenderOptions options = ConfigRenderOptions.defaults().setComments(false)
-
object
ConfigResolveOptions
A set of options related to resolving substitutions.
A set of options related to resolving substitutions. Substitutions use the
${foo.bar
syntax and are documented in the spec.Typically this class would be used with the method
Config#resolve(ConfigResolveOptions)
.This object is immutable, so the "setters" return a new object.
Here is an example of creating a custom
ConfigResolveOptions
:ConfigResolveOptions options = ConfigResolveOptions.defaults() .setUseSystemEnvironment(false)
In addition to
ConfigResolveOptions#defaults
, there's a prebuiltConfigResolveOptions#noSystem
which avoids looking at any system environment variables or other external system information. (Right now, environment variables are the only example.) - object ConfigSyntax extends Serializable
-
object
ConfigUtil
Contains static utility methods.
-
object
ConfigValueFactory
This class holds some static factory methods for building
ConfigValue
instances.This class holds some static factory methods for building
ConfigValue
instances. See alsoConfigFactory
which has methods for parsing files and certain in-memory data structures. - object ConfigValueType extends Serializable