您可以将config转换为支持配置缓存失效(以及合理默认值)的方法,因此您可以选择动态(以下示例中的默认值)和性能.
一般来说,我建议你使用TypeSafe的Config的一个好的Scala类型安全包装器,例如Ficus(例如Gradle-stype artifact dependency net.ceedubs:ficus_2.11:1.1.1)
package config
import scala.collection.concurrent.TrieMap
import com.typesafe.config.{Config, ConfigFactory}
import net.ceedubs.ficus.Ficus._
trait Settings {
protected[config] def config (
name: String = "local",
invalidateCache: Boolean = false
): Config = {
if (invalidateCache) { ConfigFactory invalidateCaches }
ConfigFactory load name
}
def engine: EngineSettings
}
trait EngineSettings {
def weight: Int
def offset: Int
}
class AppSettings(val name: String = "local") extends Settings {
val c = config()
override def engine = new EngineSettings {
override def weight = c.as[Int]("engine.weight")
override def offset = c.as[Int]("engine.offset")
}
}
object Settings {
private val namedSettings = new TrieMap[String, AppSettings]
def load(configName: String = "local"): Settings = {
// e.g.
val loadedUpToDate = new AppSettings
namedSettings +=
((configName + "." + System.currentTimeMillis, loadedUpToDate))
new Settings {
override def engine = loadedUpToDate.engine
}
}
}
我认为这解决了你的问题,因为:
>默认情况下,配置检索通过重新加载是动态的>通过使用方法,您不会诉诸可变状态