Io Scala从磁盘和资源加载文件(如果失败)
我是Scala新手,正在使用Scala和Akka开发RESTful API,需要从XML文件中读取绑定配置,该文件包含以下部分Io Scala从磁盘和资源加载文件(如果失败),io,Io,我是Scala新手,正在使用Scala和Akka开发RESTful API,需要从XML文件中读取绑定配置,该文件包含以下部分 <appconfig> <bindproperties> <host>localhost</host> <port value="7777"/> </bindproperties> </appconfig> 但这总是从嵌入JAR文件的资源文件中读取,而不
<appconfig>
<bindproperties>
<host>localhost</host>
<port value="7777"/>
</bindproperties>
</appconfig>
但这总是从嵌入JAR文件的资源文件中读取,而不是从磁盘读取
谁能指导我怎么做?我希望逻辑流程如下所示:
如果classpath/config文件夹中存在xml文件
将XML文件作为AppSettings加载
否则
将JAR中作为资源嵌入的默认资源XML文件作为AppSettings加载
从AppSettings读取属性
作为更好的选择,使用Config com.typesafe.Config加载配置。您可以将默认配置设置为reference.conf。您提到的所有设置都可以放入此配置文件中 但是,如果您想从磁盘加载XML文件。我建议在运行时在标准位置进行配置检查,包括部署目录或到部署目录的相对路径。如果此检查失败,您可以回退到嵌入式资源 同样,如果未提供外部配置,配置得分高于此值,因为您可以定义回退
val stream = getClass.getResourceAsStream("/config/PreferencesServiceConfiguration.xml")
val appSettings = scala.xml.XML.load(stream)
def bindHost : String = { (appSettings \ "bindproperties" \ "host").text.toString }
def bindPort : Int = { (appSettings \ "bindproperties" \ "port" \ "@value").toString.toInt }