我想在我的项目中使用typesafe配置(HOCON配置文件),这有助于简化和组织应用程序配置.目前我正在使用普通的Java属性文件(application.properties),这在大项目中很难处理.
我的项目是Spring MVC(不是Spring启动项目).有没有办法支持我的Spring环境(我将注入到我的服务中)以支持typesafe配置.哪个不应该制动我现有的环境用法像@Value注释@Autowired Environment等.
如何以最小的努力和我的代码更改来做到这一点.
这是我目前的解决方案:寻找还有其他更好的方法
@Configuration
public class PropertyLoader{
private static Logger logger = LoggerFactory.getLogger(PropertyLoader.class);
@Bean
@Autowired
public static PropertySourcesPlaceholderConfigurer properties(Environment env) {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Config conf = ConfigFactory.load();
conf.resolve();
TypesafePropertySource propertySource = new TypesafePropertySource("hoconSource", conf);
ConfigurableEnvironment environment = (StandardEnvironment)env;
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addLast(propertySource);
pspc.setPropertySources(propertySources);
return pspc;
}
}
class TypesafePropertySource extends PropertySource<Config>{
public TypesafePropertySource(String name, Config source) {
super(name, source);
}
@Override
public Object getProperty(String name) {
return this.getSource().getAnyRef(name);
} …Run Code Online (Sandbox Code Playgroud) 是否存在类似于C++的libconfig的Java库,其中配置文件以类似JSON的格式存储,可以由人类编辑,然后从程序中读取?
我不想使用Spring或任何更大的框架.我正在寻找的是一个小型,快速,独立的库.我看了java.util.Properties,但它似乎不支持分层/嵌套配置数据.
我有一个nodejs应用程序,它将采用JSON配置文件.
JSON文件将包含一些$ {}和#{}标记,这些标记将用于通过加载模板配置和填充标记来构建动态上下文.HOCON也可能最终被最终使用,但那还没有.
我过去遇到过Typesafe Config,这种事情看起来很神奇.我在npm周围做了一些搜索,并没有在节点世界中发现任何类似的东西,但也许我对搜索的术语太不熟悉了.
有没有人知道nodejs中的类似库或我可能在nodejs中使用的合理策略?
我知道使用JSON上的字符串替换实现某些东西并不会有太多的努力或者其他一些这样的事情虽然我不禁认为这在节点应用程序之前已经完成,并且可能以比我做得更好的方式完成它适用于这个单一用例.在此基础上,在我继续之前问这里似乎是有道理的.
我在SBT下有多模块项目.
项目A(库)具有reference.conf包含A配置参数的文件.项目A依赖于akka-actor库,它随附自己的reference.conf文件.项目A重新定义了一些akka自己的参数reference.conf.
项目B取决于A.
当我打电话ConfigFactory.load()时B,我reference.conf的合并顺序错了.首先需要A配置,然后akka-actor在其上应用配置.最终,我正在进行初始akka-actor配置.
我该如何解决?我需要akka-actor首先加载配置,然后我A的配置应该应用于它.
假设我有一个scala案例类,可以序列化为json(使用json4s或其他一些库):
case class Weather(zip : String, temp : Double, isRaining : Boolean)
Run Code Online (Sandbox Code Playgroud)
如果我正在使用HOCON配置文件:
allWeather {
BeverlyHills {
zip : 90210
temp : 75.0
isRaining : false
}
Cambridge {
zip : 10013
temp : 32.0
isRainging : true
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用typesafe配置来自动实例化Weather对象?
我正在寻找形式的东西
val config : Config = ConfigFactory.parseFile(new java.io.File("weather.conf"))
val bevHills : Weather = config.getObject("allWeather.BeverlyHills").as[Weather]
Run Code Online (Sandbox Code Playgroud)
该解决方案可以利用引用的值"allWeather.BeverlyHills"是json"blob" 的事实.
我显然可以编写自己的解析器:
def configToWeather(config : Config) =
Weather(config.getString("zip"),
config.getDouble("temp"),
config.getBoolean("isRaining"))
val bevHills = configToWeather(config.getConfig("allWeather.BeverlyHills"))
Run Code Online (Sandbox Code Playgroud)
但这似乎不够优雅,因为天气定义的任何变化也需要改变configToWeather …
在HOCON和Typesafe配置中,如何在替换时设置默认值.
它支持这样的东西?
${server.host: 'localhost'} - >如果设置了server.host(在相同的配置文件中或通过环境设置),则替换为如果未设置,则选择默认值
我正在使用 HOCON 配置格式,解析库来自 typesafe.Config。HOCON 支持环境变量注入和覆盖。喜欢:
my.config = "asd"
my.config = ${?MY_ENV_VAR}
Run Code Online (Sandbox Code Playgroud)
如果存在名为 MY_ENV_VAR 的环境变量,这将替换默认值“asd”。但是我似乎找不到任何好方法来进行列表环境替换。喜欢:
my.config = [1,2,3,4]
my.config = ${?MY_ENV_LIST}
Run Code Online (Sandbox Code Playgroud)
因为默认情况下,来自外部的环境变量将默认为字符串,因此[1,2,3,4]从库的角度来看将被视为“[1,2,3,4]”,因此它不能被视为列表并且会产生运行时错误像这样:
com.typesafe.config.ConfigException$WrongType: application.conf: 5: application.boolliststring 的类型为 STRING 而不是 LIST
在 com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:133) 在 com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145) 在 com.typesafe.config.impl.SimpleConfig.find (SimpleConfig.java:151)在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)在com.typesafe。 config.impl.SimpleConfig.getList(SimpleConfig.java:212)
任何人都知道以 HOCON 格式进行列表 env 替换的正确方法是什么?提前致谢。
我想将.conf文件直接转换为json,以便将其传递给前端.有没有办法在scala/play中这样做?我正在采取的路径似乎非常麻烦:
val conf: Configuration = play.api.Configuration.apply(ConfigFactory.parseFile(new File("app/assets/strings.conf")))
conf.entrySet.seq.map(t => t._1 -> t._2.unwrapped())
// which gives me a Seq[(String, AnyRef)] which cannot be converted with Json, so the path from here is even uglier
Run Code Online (Sandbox Code Playgroud)
我很想回到JSON,但HOCON语法非常适合我们的用例.HOCON是basicly JSON用更少的括号和引号-所以转换应该是很简单的.我还是找不到一个简单的方法来玩play/scala这样的事情.
还没有找到缩进/自动格式化HOCON配置文件的方法。你这是怎么来的?
我正在使用typesafe配置,我正在寻找一种简单的方法来允许在应用程序之间共享的配置文件中的属性的可选覆盖.
所以假设我有2个应用程序,A和B.它们都使用模块Z.它们都加载了共享配置文件'shared.conf'.
模块Z在其reference.conf中定义了一个属性
z.foo=bar
Run Code Online (Sandbox Code Playgroud)
我希望能够
#override the property for both A & B
z.foo=zap
Run Code Online (Sandbox Code Playgroud)
要么
#override the property for just A
a.z.foo=zip
Run Code Online (Sandbox Code Playgroud)
我知道我可以在每个应用程序的application.conf中只为'z'前缀执行此操作.例如
z = ${?a.z}
Run Code Online (Sandbox Code Playgroud)
但我希望可能有一种方法可以一直移动到根节点.例如
MAGICAL_ROOT = ${?a}
Run Code Online (Sandbox Code Playgroud)
我希望有可能吗?
hocon ×10
java ×4
scala ×2
autoformat ×1
config ×1
emacs ×1
indentation ×1
json ×1
node.js ×1
properties ×1
spring ×1
spring-mvc ×1
typesafe ×1