Groovy could not find matching constructor?

拟墨画扇 提交于 2020-01-05 07:51:12

问题


Please note: although this question mentions Mongo it is surely a pure Groovy question at heart.

My MyApp#bootstrap method:

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)
}

My MongoDao class (snippet):

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")
    }
}

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)

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?


回答1:


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



来源:https://stackoverflow.com/questions/25408641/groovy-could-not-find-matching-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!