gpt4 book ai didi

java - 在玩java mongodb时注入(inject)构造函数时出错

转载 作者:行者123 更新时间:2023-12-01 11:20:08 26 4
gpt4 key购买 nike

我正在尝试将我的 dao 对象注入(inject) Controller 中。我已经这样做了:我有一个:
1.MongoDBHelper
2.MerchantDAO
3.商户服务
4. MerchantController

这是 MongoDBHelper 类:

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

这是 MerchantDAO 类

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

这是 MerchantService 类:

public class MerchantService {

static final Config config = ConfigFactory.load(Play.application().configuration().getString("property.file.name"));

@Inject
MerchantDAO merchantDAO;

// Creating unique authToken for already logged in merchant
public String createToken(Merchant merchant) {
merchantDAO.updateMerchantWithAuthToken(merchant);
return authToken;
}
}

这是 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 to perform login
return ok(); // status success / failure
}
}

我收到以下错误:

ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.NullPointerException
at daos.MerchantDAO.<init>(MerchantDAO.java:22)
while locating daos.MerchantDAO
for field at services.MerchantService.merchantDAO(MerchantService.java:26)
while locating services.MerchantService
for field at controllers.MerchantController.merchantService(MerchantController.java:21)
while locating controllers.MerchantController
for parameter 2 at router.Routes.<init>(Routes.scala:36)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router

1 error

我可能做错了什么?为什么DI不能正常工作?

提前致谢。

最佳答案

我认为问题出在这些行上:

private Datastore datastore = mongoDBHelper.getDatastore();
private DB db = mongoDBHelper.getDB();

这些是在对象实例的构造期间评估的。我相信在对象实例完成构造之后才会发生注入(inject)。因此,在进行上述分配时,mongoDBHelper 为 null。

解决此问题的一种方法是在 updateMerchantWithAuthToken 方法中设置数据存储和数据库。

关于java - 在玩java mongodb时注入(inject)构造函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31346684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com