java重新配置_java – 实现重新加载Typesafe配置的好方法是什么

您可以将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

}

}

}

我认为这解决了你的问题,因为:

>默认情况下,配置检索通过重新加载是动态的>通过使用方法,您不会诉诸可变状态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值