Hi,
On Fri, May 25, 2012 at 8:45 AM, Derek Wyatt <
de...@derekwyatt.org> wrote:
> Thx. So either way would be the "right" way and one can choose whatever
> they like best. Looks good.
>
I would argue that doing only the parseString is bad.
1. ConfigFactory.load supports system property overrides
(-Dfoo.bar=whatever) for config settings.
2. With only the parseString (or parseWhatever), if you have any
substitutions (the ${} syntax in the config file), the reference
config is not present for resolving the ${}.
3. If the "root" main() method of an app creates a complete config, it
can pass that down to any and all libraries such as Akka, and it won't
need to be modified or copied by each library. (The withFallback code
in ActorSystem will no-op, after wasting some CPU, if the config is
already complete.)
4. If you have one config for the whole app, the config for each
library can refer to config settings for other libs, or you can have
app-wide config settings. Each lib is not isolated in its own little
world.
5. ConfigFactory.load may be slightly more future-proof, for example
new ways to override or provide defaults could appear in a future
version.
These issues may or may not make an immediate practical difference for
a particular app. But as far as "what's recommended," my view is that
you should pass in a complete config (including reference config) to
ActorSystem.
In some sense the
"cfg.withFallback(ConfigFactory.defaultReference(classLoader))" in
ActorSystem is papering over apps that don't follow best practices. If
that line weren't present, the checkValid following would throw an
exception complaining about missing defaults. I think it would be
legitimate for Akka to have done that instead, and the sample provided
at
https://github.com/typesafehub/config/blob/master/examples/simple-lib/src/main/scala/simplelib/SimpleLib.scala
does throw an exception here. Akka is just trying to be nice and
rescue suboptimal apps ;-)
If you want to add a couple settings in code with parseString or
parseMap, I would suggest the approach described in "Reading
configuration from a custom location" in the docs snapshot:
http://doc.akka.io/docs/akka/snapshot/general/configuration.html
(the current release 2.0.1 docs don't have that section)
If you use ConfigFactory.load() with no parameters and then merge your
in-code settings with that, you retain the ability to specify
-Dconfig.file etc. on the command line ... if you use
ConfigFactory.load(something), then you replace the default (which is
-Dconfig.{file,resource,url} or if those are not present,
application.conf on classpath).
Havoc