Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么时候在Objectify中为GAE注册类?_Java_Google App Engine_Objectify - Fatal编程技术网

Java 什么时候在Objectify中为GAE注册类?

Java 什么时候在Objectify中为GAE注册类?,java,google-app-engine,objectify,Java,Google App Engine,Objectify,因此,这可能是一个愚蠢的问题,但您何时向注册类: ObjectifyService.register( User.class ); 目前,我正在一个接口类的构造函数中进行此操作,我在其他类中使用该类来简化数据存储的使用,特别是在我的应用程序中。但是,我遇到了以下错误: 尝试注册种类“用户”两次 所以,我想我的问题是,在Objectify中注册类的频率和具体时间是多少 谢谢 另外,这是我的全班同学: import java.security.InvalidKeyException; import

因此,这可能是一个愚蠢的问题,但您何时向注册类:

ObjectifyService.register( User.class );
目前,我正在一个接口类的构造函数中进行此操作,我在其他类中使用该类来简化数据存储的使用,特别是在我的应用程序中。但是,我遇到了以下错误:

尝试注册种类“用户”两次

所以,我想我的问题是,在Objectify中注册类的频率和具体时间是多少

谢谢

另外,这是我的全班同学:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.persistence.Id;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;

public class UsersService {

    Objectify ojy;

    public UsersService(){
        ObjectifyService.register( User.class );
        ojy = ObjectifyService.begin();
    }

    public void regUser(String email, String password, String firstName, String lastName){
        //TODO: Check syntax if email
        //TODO: store encrypted password
    }

    public void regUser(String email, String password, String firstName){
        regUser(email, password, firstName, null);
    }

    public void regUser(String email, String password){
        regUser(email, password, "", "");
    }

    public boolean checkFor(Long acc_id){
        User checked_user = ojy.find(User.class, acc_id);
        if(checked_user == null){
            return false;
        }else{
            return true;
        }
    }

    public User getUser(String email, String password) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException{
        String pass_enc = MyUtils.getEncrypted(password);
        Iterable<User> users = ojy.query(User.class).filter("email", email).filter("password", pass_enc);
        Iterator<User> iter = users.iterator();
        if(iter.hasNext()){
            return iter.next();
        }else{
            return null;
        }
    }

}
import java.security.InvalidKeyException;
导入java.security.NoSuchAlgorithmException;
导入java.util.Iterator;
导入javax.crypto.BadPaddingException;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.NoSuchPaddingException;
导入javax.persistence.Id;
导入com.googlecode.objectify.objectify;
导入com.googlecode.objectify.ObjectifyService;
导入com.googlecode.objectify.annotation.index;
导入com.googlecode.objectify.annotation.unindex;
公共类用户服务{
客观化ojy;
公共用户服务(){
ObjectifyService.register(User.class);
ojy=ObjectifyService.begin();
}
public void regUser(字符串电子邮件、字符串密码、字符串名字、字符串姓氏){
//TODO:检查电子邮件的语法
//TODO:存储加密密码
}
public void regUser(字符串电子邮件、字符串密码、字符串名字){
regUser(电子邮件、密码、名字、空);
}
public void regUser(字符串电子邮件、字符串密码){
regUser(电子邮件,密码,“,”);
}
公共布尔校验(长acc_id){
用户已选中\u User=ojy.find(User.class,acc\u id);
如果(选中用户==null){
返回false;
}否则{
返回true;
}
}
公共用户getUser(字符串电子邮件、字符串密码)抛出InvalidKeyException、IllegalBlockSizeException、BadPaddingException、NoSuchAlgorithmException、NoSuchPaddingException{
字符串pass\u enc=MyUtils.getEncrypted(密码);
Iterable users=ojy.query(User.class).filter(“电子邮件”,email.filter(“密码”,pass\u enc));
迭代器iter=users.Iterator();
if(iter.hasNext()){
返回iter.next();
}否则{
返回null;
}
}
}

更新

以下是最佳实践解决方案:

使用您自己的服务, 这可以保证在使用Objectify之前注册实体,但不一定会影响不访问数据存储的请求的应用程序启动

然后像这样使用它:

public User createUser(User pUser) {

    Objectify objectify = OfyService.ofy();
    objectify.put(pUser);

    return pUser;
}
static{
    ObjectifyService.register( User.class );
}
@Autowired
@Qualifier("objectifyService")
OfyService objectifyService;
objectifyService.ofy().save().entity(user).now();

原始答案(最好使用上述代码):

您应该在类中这样做,只需放置一个静态块,如下所示:

public User createUser(User pUser) {

    Objectify objectify = OfyService.ofy();
    objectify.put(pUser);

    return pUser;
}
static{
    ObjectifyService.register( User.class );
}
@Autowired
@Qualifier("objectifyService")
OfyService objectifyService;
objectifyService.ofy().save().entity(user).now();
p、 s,你也来看看对象化的最佳实践


我使用
@实体
注释、库和运行时注册,对我的任何应用程序的启动时间都没有重大影响,因为所有信息都是在编译/构建时收集的

包com.vertigrated.servlet;
导入com.google.appengine.api.ThreadManager;
导入com.googlecode.objectify.objectify工厂;
导入com.googlecode.objectify.ObjectifyService;
导入com.googlecode.objectify.annotation.Entity;
导入org.reflections.reflections;
导入org.reflections.util.ClasspathHelper;
导入org.reflections.util.ConfigurationBuilder;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入javax.annotation.Nonnull;
导入javax.servlet.ServletContextEvent;
导入javax.servlet.ServletContextListener;
导入java.util.HashSet;
导入java.util.Set;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
/**
*此类处理Objectify中带有@Entity或@Subclass注释的类的类路径
*并将它们注册到ObjectifyFactory,多线程使用预构建的类列表进行处理
*在编译时由Reflections库创建,运行速度非常快!
*/
公共类ObjectifyLoaderContextListener实现ServletContextListener
{
私有静态最终记录器L=LoggerFactory.getLogger(ObjectifyLoaderContextListener.class);

私有最终集基于Danie的回答,如果其他人正在使用依赖注入,我为Spring MVC做了这件事,效果很好:

我创建了一个服务,如下所示:

@Service
@Qualifier("objectifyService")
public class OfyService {
    static {
        ObjectifyService.register(GaeUser.class);
    }

    public static Objectify ofy() {
        return ObjectifyService.ofy();
    }

    public static ObjectifyFactory factory() {
        return ObjectifyService.factory();
    }

}
然后,每当我想使用它时,我就这样注入服务:

public User createUser(User pUser) {

    Objectify objectify = OfyService.ofy();
    objectify.put(pUser);

    return pUser;
}
static{
    ObjectifyService.register( User.class );
}
@Autowired
@Qualifier("objectifyService")
OfyService objectifyService;
objectifyService.ofy().save().entity(user).now();
然后我就这样使用它:

public User createUser(User pUser) {

    Objectify objectify = OfyService.ofy();
    objectify.put(pUser);

    return pUser;
}
static{
    ObjectifyService.register( User.class );
}
@Autowired
@Qualifier("objectifyService")
OfyService objectifyService;
objectifyService.ofy().save().entity(user).now();

谢谢。我会研究一下,然后再回来。它很有效!只是出于好奇,静态块和构造函数之间有什么区别?静态块在类加载时执行一次(不创建对象)更多信息:谢谢!这真的很有帮助。@怀疑者,您一定是在错误的位置查看了正确的页面…因为当前问题中的示例涉及DAO…我给出了一个关于如何在DAO中执行此操作的示例…我没有告诉您将静态块放置在用户类中,而是说将其放置在已发布的类中在问题中。。。