scala 找不到密钥类型安全配置的配置设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27972232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:50:05  来源:igfitidea点击:

No configuration setting found for key typesafe config

scalatypesafe-config

提问by MIkCode

Im trying to implement a configuration tool typesafehub/configim using this code

我正在尝试 使用此代码实现配置工具typesafehub/config

 val conf = ConfigFactory.load()
 val url = conf.getString("add.prefix") + id + "/?" + conf.getString("add.token")

And the location of the property file is /src/main/resources/application.conf

并且属性文件的位置是 /src/main/resources/application.conf

But for some reason i'm receiving

但出于某种原因,我收到

com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'add'

File content

文件内容

add {
  token = "access_token=6235uhC9kG05ulDtG8DJDA"
  prefix = "https://graph.facebook.com/v2.2/"
  limit = "&limit=250"
  comments="?pretty=0&limit=250&access_token=69kG05ulDtG8DJDA&filter=stream"
  feed="/feed?limit=200&access_token=623501EuhC9kG05ulDtG8DJDA&pretty=0"
}

Everything looks configured correctly ?? do i missed something .

一切看起来都配置正确?我错过了什么吗。

thanks,

谢谢,

miki

三木

回答by Reid Spencer

The error message is telling you that whatever configuration got read, it didn't include a top level setting named add. The ConfigFactory.loadfunction will attempt to load the configuration from a variety of places. By default it will look for a file named applicationwith a suffix of .confor .json. It looks for that file as a Java resource on your class path. However, various system properties will override this default behavior.

错误消息告诉您无论读取什么配置,它都不包含名为add. 该ConfigFactory.load函数将尝试从多个位置加载配置。默认情况下,它将查找以或application为后缀的文件。它会将该文件作为类路径上的 Java 资源进行查找。但是,各种系统属性将覆盖此默认行为。.conf.json

So, it is likely that what you missed is one of these:

因此,您错过的很可能是其中之一:

  • Is it possible that src/main/resourcesis not on your class path?
  • Are the config.file, config.resourceor config.urlproperties set?
  • Is your application.conffile empty?
  • Do you have an application.confthat would be found earlier in your class path?
  • Is the key: adddefined in the application.conf?
  • 这可能src/main/resources不在您的课程路径上吗?
  • 是否设置了config.file,config.resourceconfig.url属性?
  • 你的application.conf文件是空的吗?
  • 你有一个application.conf会在你的课程路径中更早找到的吗?
  • key:add是在application.conf?

回答by Tim Malt

Are you using an IDE or sbt? I had a similar problem while using Eclipse. It simply did not find the application.conf file at first and later on failed to notice edits. However, once I ran my program via sbt, all worked just fine, including Eclipse. So, I added 'main/resources' to the libraries (Project -> Properties -> Java Build Path -> Libraries", "add class folder"). That might help you as well.

您使用的是 IDE 还是 sbt?我在使用 Eclipse 时遇到了类似的问题。它起初只是没有找到 application.conf 文件,后来没有注意到编辑。然而,一旦我通过 sbt 运行我的程序,一切都很好,包括 Eclipse。因此,我在库中添加了“main/resources”(项目 -> 属性 -> Java 构建路径 -> 库”、“添加类文件夹”)。这也可能对您有所帮助。

回答by Deepesh Rehi

Place your application.conf in the src folder and it should work

将您的 application.conf 放在 src 文件夹中,它应该可以工作

回答by jsears

I ran into this issue inside a Specs2 test that was driven by SBT. It turned out that the issue was caused by https://github.com/etorreborre/specs2/issues/556. In that case, the Thread's contextClassLoader wasn't using the correct classloader. If you run into a similar error, there are other versions of ConfigFactory.load() that allow you to pass the current class's ClassLoader instead. If you're using Specs2 and you're seeing this issue, use a version <= 3.8.6 or >= 4.0.1.

我在 SBT 驱动的 Specs2 测试中遇到了这个问题。原来问题是由https://github.com/etorreborre/specs2/issues/556引起的。在那种情况下,线程的 contextClassLoader 没有使用正确的类加载器。如果遇到类似的错误,还有其他版本的 ConfigFactory.load() 允许您改为传递当前类的 ClassLoader。如果您使用的是 Specs2 并且遇到此问题,请使用 <= 3.8.6 或 >= 4.0.1 的版本。

回答by AlexPes

Check you path. In my case I got the same issue, having application.conf placed in src/main/resources/configuration/common/application.conf

检查你的路径。就我而言,我遇到了同样的问题,将 application.conf 放在 src/main/resources/configuration/common/application.conf

Incorrect:

不正确:

val conf = ConfigFactory.load(s"/configuration/common/application.conf")

Correct

正确的

val conf = ConfigFactory.load(s"configuration/common/application.conf")

it turned out to be a silly mistake i made.

结果证明这是我犯的一个愚蠢的错误。

Following that, i does not matter if you use ":" or "=" in .conf file.

之后,我在 .conf 文件中使用“:”还是“=”并不重要。

Getting the value from example:

从示例中获取值:

server{
       proc {
             max = "600"
            }
      }
conf.getString("server.proc.max")

Even you can have the following conf:

即使您可以拥有以下配置:

 proc {
                 max = "600"
                }
 proc {
                main = "60000"
                }
conf.getString("proc.max") //prints 600

conf.getString("proc.min") //prints 60000

回答by ZURA Tikaradze

in my case it was a stupid mistake, i m change file name from "application.config" to "application.conf" and its works .

在我的情况下,这是一个愚蠢的错误,我将文件名从“application.config”更改为“application.conf”及其工作原理。

回答by Def_Os

I ran into this doing a getStringon an integer in my configuration file.

getString在我的配置文件中对一个整数做 a时遇到了这个问题。

回答by koleS

I ran into exactly the same problem and the solution was to replace =with :in the application.conf. Try with the following content in your application.conf:

我遇到了一模一样的问题和解决办法是更换=:application.conf。尝试在您的application.conf:

add {
  token: "access_token=6235uhC9kG05ulDtG8DJDA"
  prefix: "https://graph.facebook.com/v2.2/"
  limit: "&limit=250"
  comments: "?pretty=0&limit=250&access_token=69kG05ulDtG8DJDA&filter=stream"
  feed: "/feed?limit=200&access_token=623501EuhC9kG05ulDtG8DJDA&pretty=0"
}

Strangely, IntelliJ doesn't detect any formatting or syntax error when using =for me.

奇怪的是,IntelliJ 在=为我使用时没有检测到任何格式或语法错误。