lightbend / config

configuration library for JVM languages using HOCON files
https://lightbend.github.io/config/
6.12k stars 968 forks source link

Something error when use com.typesafe.config API convert conf file to scala bean class #792

Open dragonpic1988 opened 1 year ago

dragonpic1988 commented 1 year ago

version 1.4.0 Did I make a mistake? What is the correct way to do this? scala code: object AppMain { def main(args: Array[String]): Unit = { val conf = ConfigFactory.parseFile(new File("./center/app.conf")) val app = ConfigBeanFactory.create(conf, classOf[App]) println("hello=" + app) } }

case class App( config: Config ) { def run(): Unit = { println(config.run()) } }

case class Config( url: String, safe: Boolean, count: Int, users: List[String] ) { def run(): Unit = { println("url=" + url) println("safe=" + safe) println("count=" + count) println("users=" + users) } } then, error: Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:64) Caused by: com.typesafe.config.ConfigException$BadBean: com.saic.autobot.App needs a public no-args constructor to be used as a bean at com.typesafe.config.impl.ConfigBeanImpl.createInternal(ConfigBeanImpl.java:129) at com.typesafe.config.ConfigBeanFactory.create(ConfigBeanFactory.java:47) at com.saic.autobot.AppMain$.main(AppMain.scala:13) at com.saic.autobot.AppMain.main(AppMain.scala) ... 5 more Caused by: java.lang.InstantiationException: com.saic.autobot.App at java.lang.Class.newInstance(Class.java:427) at com.typesafe.config.impl.ConfigBeanImpl.createInternal(ConfigBeanImpl.java:109) ... 8 more Caused by: java.lang.NoSuchMethodException: com.saic.autobot.App.() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 9 more