Groovy找不到匹配的构造函数? [英] Groovy could not find matching constructor?
问题描述
请注意:,尽管该问题提到了Mongo,但它的确是一个纯粹的Groovy问题.
Please note: although this question mentions Mongo it is surely a pure Groovy question at heart.
我的MyApp#bootstrap
方法:
def bootstrap(AppConfiguration config) {
String h = config.dbHost
String p = config.dbPort
println "Mongo is at: ${h}:${p}."
dao = new MongoDao(host: h, port: p)
}
我的MongoDao
类(摘要):
class MongoDao implements BasicDao {
String dbName
Mongo mongo
String host
String port
Morphia morphia
Datastore datastore
MongoDao(String host, String port) {
this.dbName = "db_myapp"
this.mongo = new Mongo(host, port)
this.morphia = new Morphia()
this.datastore = morphia.createDatastore(mongo, dbName)
morphia.mapPackage("myappdb.common")
}
}
此bootstrap()
方法运行时,出现以下异常:
When this bootstrap()
method runs I get the following exception:
Mongo is at: mymongo01:27017.
Exception in thread "main" groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.me.myapp.dao.MongoDao(java.util.LinkedHashMap)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1601)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1404)
at org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite.callConstructor(MetaClassConstructorSite.java:46)
这是怎么回事?刚刚找到主机/端口并打印到STDOUT的感觉如何,但是当我们构造DAO时,它们神奇地变成了LinkedHashMap
?
What is going on here? How is it that the host/port are read in and print to STDOUT just find but then when we construct the DAO, they magically turn into a LinkedHashMap
?
推荐答案
如果您要调用一个名为args的构造函数,则您的类还必须提供一个无参数的构造函数.
if you want to call a constructor with named args, your class MUST provide also a no-arg constructor.
在您的情况下,我会打以下电话:
in your case, I'd go for the following call:
dao = new MongoDao( h, p )
因为此构造函数正在做某事
as this constructor is doing some job
这篇关于Groovy找不到匹配的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!