Recently I am reading some scala code which uses Guice to inject the Typesafe Config.. It seems kind of magic to me how this works.. My question is, how to interpret this code? Does Guice inject all those configuration values read in sbt-assembly into typesafe config automatically?
Scala code:
class FooImpl @Inject() (
config: Config
) extends Foo {
private val myConfig = "section.foo"
override val batchSize = config.getInt(s"$myConfig.batchSize")
.....
}
In Setting.scala
object Settings {
...
assemblyMergeStrategy in assembly := {
case "prod.conf" => MergeStrategy.concat
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}
...
In prod.conf
section {
foo {
batchSize = 10000
...
I think you're mixing three different mechanisms here :)
@Inject is indeed Guice, and it's a final step in the process. Simply put, Guice has a "dependency injection container" that knows where to look for instances of certain types. One of the types it knows of is the Config
. How it knows this depends on the framework you're using (or how you instantiate your Guice container if you're not using it);
Typesafe config has rules on where to look for configuration. Readme sums it pretty good, but in short - it finds application.conf in resources folder (or, actually, anywhere on the classpath), and then imports all the other files that application.conf
explicitly imports (using import other_conf.conf
). I'm assuming in your case there's import prod.conf
somewhere in the application.conf
Assembly - just puts all the resources from all the dependencies into one giant resource folder - specifying rules on what to do if there are multiple files wit hthe same name. In your case, it tells it to just concatenate them.