首页 > 解决方案 > Scala Akka 项目中 .properties 文件的位置

问题描述

我有一个 akka 项目,其中我正在Properties使用代码构建一个对象:

   import java.io.FileNotFoundException
   import java.util.Properties
   import scala.io.Source

    val configPath = "hikari.properties"

    val url = getClass.getResource(hikariConfigPath)

    val properties: Properties = new Properties()

    if (url != null) {

      val source = Source.fromURL(url)

      properties.load(source.bufferedReader())

    } else {

      println("properties file cannot be loaded at path " + configPath)

      throw new FileNotFoundException("Properties file cannot be loaded”)

    }

  

这给了我“无法在路径 hikari.properties 加载属性文件”</p>

有这个特例:

java.io.FileNotFoundException: Properties file cannot be loaded
    at model.daos.JavaConnectionFactoryBuilder$.buildDataSource(JavaConnectionFactory.scala:25)

我已按照https://zetcode.com/articles/hikaricp/的建议将hikari.properties文件放在目录中src/main/resources

我该如何解决这个问题?

标签: scalaakka

解决方案


您正在加载一个不存在的文件hikari.而不是hikari.properties.

它应该是:

 val hikariConfigPath = "hikari.propertiez"

推荐阅读