mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
In the examples, also show using the config for the app itself
This commit is contained in:
parent
9af5a67d6c
commit
4964fdcbf0
@ -1,2 +1,8 @@
|
||||
# these are our own config values defined by the app
|
||||
complex-app {
|
||||
something="This value comes from complex-app's complex1.conf"
|
||||
}
|
||||
|
||||
# Here we override some values used by a library
|
||||
simple-lib.foo="This value comes from complex-app's complex1.conf"
|
||||
simple-lib.whatever = "This value comes from complex-app's complex1.conf"
|
@ -1,8 +1,9 @@
|
||||
complex-app {
|
||||
something="This value comes from complex-app's complex2.conf"
|
||||
|
||||
# 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="This value comes from complex-app's complex2.conf in its custom simple-lib-context"
|
||||
|
@ -2,20 +2,44 @@ import com.typesafe.config._
|
||||
import simplelib._
|
||||
|
||||
object ComplexApp extends App {
|
||||
|
||||
// This app is "complex" because we load multiple separate app
|
||||
// configs into a single JVM and we have a separately-configurable
|
||||
// context for simple lib.
|
||||
|
||||
// using a custom Config with the simple-lib library
|
||||
// (simple-lib is a library in this same examples/ directory)
|
||||
def demoConfigInSimpleLib(config: Config) {
|
||||
val context = new SimpleLibContext(config)
|
||||
context.printSetting("simple-lib.foo")
|
||||
context.printSetting("simple-lib.hello")
|
||||
context.printSetting("simple-lib.whatever")
|
||||
}
|
||||
|
||||
// system property overrides work, but the properties must be set
|
||||
// before the config lib is used (config lib will not notice changes
|
||||
// once it loads the properties)
|
||||
System.setProperty("simple-lib.whatever", "This value comes from a system property")
|
||||
|
||||
///////////
|
||||
|
||||
// "config1" is just an example of using a file other than application.conf
|
||||
val config1 = ConfigFactory.load("complex1")
|
||||
|
||||
// use the config ourselves
|
||||
println("config1, complex-app.something=" + config1.getString("complex-app.something"))
|
||||
|
||||
// use the config for a library
|
||||
demoConfigInSimpleLib(config1)
|
||||
|
||||
//////////
|
||||
|
||||
// "config2" shows how to configure a library with a custom settings subtree
|
||||
val config2 = ConfigFactory.load("complex2");
|
||||
|
||||
// use the config ourselves
|
||||
println("config2, complex-app.something=" + config2.getString("complex-app.something"))
|
||||
|
||||
// pull out complex-app.simple-lib-context and move it to
|
||||
// the toplevel, creating a new config suitable for our SimpleLibContext.
|
||||
// The defaultOverrides() have to be put back on top of the stack so
|
||||
@ -23,7 +47,7 @@ object ComplexApp extends App {
|
||||
// We fall back to config2 again to be sure we get simple-lib's
|
||||
// reference.conf plus any other settings we've set. You could
|
||||
// also just fall back to ConfigFactory.referenceConfig() if
|
||||
// you don't want application.conf settings outside of
|
||||
// you don't want complex2.conf settings outside of
|
||||
// complex-app.simple-lib-context to be used.
|
||||
val simpleLibConfig2 = ConfigFactory.defaultOverrides()
|
||||
.withFallback(config2.getConfig("complex-app.simple-lib-context"))
|
||||
@ -31,7 +55,9 @@ object ComplexApp extends App {
|
||||
|
||||
demoConfigInSimpleLib(simpleLibConfig2)
|
||||
|
||||
// Now let's illustrate that simple-lib will get upset if we pass it
|
||||
//////////
|
||||
|
||||
// Here's an illustration that simple-lib will get upset if we pass it
|
||||
// a bad config. In this case, we'll fail to merge the reference
|
||||
// config in to complex-app.simple-lib-context, so simple-lib will
|
||||
// point out that some settings are missing.
|
||||
@ -41,11 +67,4 @@ object ComplexApp extends App {
|
||||
case e: ConfigException.ValidationFailed =>
|
||||
println("when we passed a bad config to simple-lib, it said: " + e.getMessage)
|
||||
}
|
||||
|
||||
def demoConfigInSimpleLib(config: Config) {
|
||||
val context = new SimpleLibContext(config)
|
||||
context.printSetting("simple-lib.foo")
|
||||
context.printSetting("simple-lib.hello")
|
||||
context.printSetting("simple-lib.whatever")
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,8 @@
|
||||
# these are our own config values defined by the app
|
||||
simple-app {
|
||||
answer=42
|
||||
}
|
||||
|
||||
# Here we override some values used by a library
|
||||
simple-lib.foo="This value comes from simple-app's application.conf"
|
||||
simple-lib.whatever = "This value comes from simple-app's application.conf"
|
||||
|
@ -2,11 +2,19 @@ import com.typesafe.config._
|
||||
import simplelib._
|
||||
|
||||
object SimpleApp extends App {
|
||||
// example of how system properties override
|
||||
// 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
|
||||
val conf = ConfigFactory.load()
|
||||
println("The answer is: " + conf.getString("simple-app.answer"))
|
||||
|
||||
// In this simple app, we're allowing SimpleLibContext() to
|
||||
// use the default config in application.conf
|
||||
// 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)
|
||||
val context = new SimpleLibContext()
|
||||
context.printSetting("simple-lib.foo")
|
||||
context.printSetting("simple-lib.hello")
|
||||
|
Loading…
Reference in New Issue
Block a user