i'm trying inject dao object in controller. i've done this: i've a:
1. mongodbhelper
2. merchantdao
3. merchantservice
4. merchantcontroller
this mongodbhelper class:
import javax.inject.singleton; @singleton public class mongodbhelper { private db db; private datastore datastore; private configuration config = play.application().configuration(); private final string server_url = config.getstring("server_url"); private final string username = config.getstring("database.username"); private final string password = config.getstring("database.password"); private final string database_name = config.getstring("database.name"); public mongodbhelper() { try { mongoclient mongoclient = new mongoclient(); this.db = mongoclient.getdb(database_name); this.db.authenticate(username, password.tochararray()); morphia morphia = new morphia(); this.datastore = morphia.createdatastore(mongoclient, database_name); morphia.mappackage("models"); } catch (unknownhostexception e) { e.printstacktrace(); } } public db getdb() { return this.db; } public datastore getdatastore() { return this.datastore; } } this merchantdao class
public class merchantdao { @inject mongodbhelper mongodbhelper; private datastore datastore = mongodbhelper.getdatastore(); private db db = mongodbhelper.getdb(); private static final string auth_token = "authtoken"; private static final config config = configfactory.load(play.application().configuration().getstring("property.file.name")); public void updatemerchantwithauthtoken(merchant merchant){ query<merchant> query = datastore.createquery(merchant.class).field(config.getstring("string.email")).equal(merchant.getemail()); updateoperations<merchant> ops = datastore.createupdateoperations(merchant.class).set(auth_token, merchant.getauthtoken()).set("lastrequesttime",merchant.getlastrequesttime()); updateresults res = datastore.update(query, ops); } } } this merchantservice class:
public class merchantservice { static final config config = configfactory.load(play.application().configuration().getstring("property.file.name")); @inject merchantdao merchantdao; // creating unique authtoken logged in merchant public string createtoken(merchant merchant) { merchantdao.updatemerchantwithauthtoken(merchant); return authtoken; } } this merchantcontroller
import javax.inject.inject; public class merchantcontroller extends controller { @inject merchantservice merchantservice; public final static string auth_token_header = "x-auth-token"; public static final string auth_token = "authtoken"; public static final config config = configfactory.load(play.application().configuration().getstring("property.file.name")); public static merchant getmerchant() { return (merchant)http.context.current().args.get("merchant"); } public result login() throws exception { // code perform login return ok(); // status success / failure } } i'm getting following error:
provisionexception: unable provision, see following errors: 1) error injecting constructor, java.lang.nullpointerexception @ daos.merchantdao.<init>(merchantdao.java:22) while locating daos.merchantdao field @ services.merchantservice.merchantdao(merchantservice.java:26) while locating services.merchantservice field @ controllers.merchantcontroller.merchantservice(merchantcontroller.java:21) while locating controllers.merchantcontroller parameter 2 @ router.routes.<init>(routes.scala:36) while locating router.routes while locating play.api.inject.routesprovider while locating play.api.routing.router 1 error what possibly doing wrong? why di not working properly?
thanks in advance.
i think problem these lines:
private datastore datastore = mongodbhelper.getdatastore(); private db db = mongodbhelper.getdb(); these evaluated during object instance's construction. believe injection won't occur until after object instance has completed construction. therefore, mongodbhelper null while above assignments made.
one way solve set datastore , db in method updatemerchantwithauthtoken.
Comments
Post a Comment