I'm trying to define a bean for class that will handle connections to mongodb beans definition
beans = {
dbCon(DbConnImpl){
mongoClient = ref("mongo_client")
dbName = "myDb"
}
mongo_client(MongoClient, "localhost", 27017)
}
DbConnImpl:
class DbConnImpl {
MongoClient mongoClient
String dbName
public DB getDB(){
return mongoClient.getDB(dbName)
}
void setMongoClient(MongoClient mongoClient) {
this.mongoClient = mongoClient
}
void setDbName(String dbName) {
this.dbName = dbName
}
}
and the usage:
class UserController {
DbConnImpl dbConn
def index() {
DB db = dbConn.getDB()
def colls = db.getCollectionNames()
render colls
}
}
error: NullPointerException : Cannot invoke method getDB() on null object
Can someone please advise? Thanks! Roy
The name of your bean is dbCon
so you need the same name in the controller:
class UserController {
DbConnImpl dbCon
}