我有以下模型:
public class Users {
public static PlayJongo jongo = Play.current().injector().instanceOf(PlayJongo.class);
public static MongoCollection users() {
return jongo.getCollection("DB.users");
}
..
..
public static Users authenticate(String email, String password) {
Users user = users().findOne("{email: #, removed: false}", email).as(Users.class);
if (user != null) {
if (HomeController.checkPassword(password, user.password)) {
return user;
}
}
return null;
}
..
我在我的控制器中使用它作为:
public Result authenticate() {
DynamicForm requestData = Form.form().bindFromRequest();
String email = requestData.get("email").trim();
String password = requestData.get("password").trim();
Users user = Users.authenticate(email, password);
if (user == null) {
flash("danger", "Incorrect email or password.");
return redirect(routes.HomeController.login());
}
session("email", user.getEmail());
session("role", user.getRole());
session("fullname", user.getLastname() + " " + user.getFirstname());
session("id", user.getId().toString());
return redirect(routes.HomeController.index());
}
我尝试了很多组合来使用 play-jongo 注入但没有结果。例如
@Inject
public PlayJongo jongo;
public MongoCollection users() {
return jongo.getCollection("DocBox.users");
}
我进入了一个静态/非静态引用的上下文错误循环。如果我删除所有静态声明,我将无法调用Users.method。如果我尝试将用户注入控制器
public class HomeController extends Controller {
@Inject
public Users users;
.
.
并尝试调用用户方法:
Users user = users.authenticate(email, password);
我收到 org.jongo.marshall.MarshallingException。我的大脑肯定是过热了,有人可以解释一下如何将 Injection 与 play-jongo 一起使用吗?