我在类路径中有一个defaults.conf。
conf {
name = "default"
title = "Simple Title"
combined = ${actor_name} ${conf.title}
}
下面是使用ConfigFactory.load
的代码,该代码可以将combined
解析为 Myactor简单标题
import com.typesafe.config.*;
public static void main(String[] args) throws Exception {
System.setProperty("actor_name","Myactor");
Config defaultsConfig = ConfigFactory.load("test/defaults.conf");
System.out.println(defaultsConfig);
}
现在,我将配置移到项目application.conf外部-
我正在尝试通过替换来解决资源-
import com.typesafe.config.*;
public static void main(String[] args) throws Exception {
System.setProperty("actor_name","Myactor");
Config cfg = ConfigFactory.parseString(new String(Files.readAllBytes(Paths.get("./src/main/application.conf")))).resolve();
System.out.println(cfg);
}
运行程序时,出现以下异常-
线程“ main”中的异常com.typesafe.config.ConfigException $ UnresolvedSubstitution:字符串:3:无法将替换替换为值:$ {actor_name} 在com.typesafe.config.impl.ConfigReference.resolveSubstitutions(ConfigReference.java:111) 在com.typesafe.config.impl.ResolveContext.realResolve(ResolveContext.java:179) 在com.typesafe.config.impl.ResolveContext.resolve(ResolveContext.java:142) 在com.typesafe.config.impl.SimpleConfigObject $ ResolveModifier.modifyChildMayThrow(SimpleConfigObject.java:380) 在com.typesafe.config.impl.SimpleConfigObject.modifyMayThrow(SimpleConfigObject.java:313) 在com.typesafe.config.impl.SimpleConfigObject.resolveSubstitutions(SimpleConfigObject.java:399) 在com.typesafe.config.impl.ResolveContext.realResolve(ResolveContext.java:179) 在com.typesafe.config.impl.ResolveContext.resolve(ResolveContext.java:142) 在com.typesafe.config.impl.SimpleConfigObject $ ResolveModifier.modifyChildMayThrow(SimpleConfigObject.java:380) 在com.typesafe.config.impl.SimpleConfigObject.modifyMayThrow(SimpleConfigObject.java:313) 在com.typesafe.config.impl.SimpleConfigObject.resolveSubstitutions(SimpleConfigObject.java:399) 在com.typesafe.config.impl.ResolveContext.realResolve(ResolveContext.java:179) 在com.typesafe.config.impl.ResolveContext.resolve(ResolveContext.java:142) 在com.typesafe.config.impl.SimpleConfigObject $ ResolveModifier.modifyChildMayThrow(SimpleConfigObject.java:380) 在com.typesafe.config.impl.SimpleConfigObject.modifyMayThrow(SimpleConfigObject.java:313) 在com.typesafe.config.impl.SimpleConfigObject.resolveSubstitutions(SimpleConfigObject.java:399) 在com.typesafe.config.impl.ResolveContext.realResolve(ResolveContext.java:179) 在com.typesafe.config.impl.ResolveContext.resolve(ResolveContext.java:142) 在com.typesafe.config.impl.ResolveContext.resolve(ResolveContext.java:231) 在com.typesafe.config.impl.SimpleConfig.resolveWith(SimpleConfig.java:78) 在com.typesafe.config.impl.SimpleConfig.resolve(SimpleConfig.java:68) 在com.typesafe.config.impl.SimpleConfig.resolve(SimpleConfig.java:63) 在com.typesafe.config.impl.SimpleConfig.resolve(SimpleConfig.java:41) 在TSConfigLoaderExample.main(TSConfigLoaderExample.java:14)
如何解析使用parseString解析配置的属性? 注意:我使用的是Typesafe配置版本1.3.4