[英]When do you register classes in Objectify for GAE?
所以這可能是一個愚蠢的問題,但你什么時候注冊類:
ObjectifyService.register( User.class );
目前,我在類似接口的類的構造函數中這樣做,我在其他類中使用它來簡化數據存儲的使用,特別是我的應用程序。 但是,我收到此錯誤:
嘗試兩次注冊“用戶”
所以,我想我的問題是你在Objectify中注冊課程的頻率和具體時間是多少?
謝謝!
PS這是我的全班:
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;
}
}
}
更新
這是最佳實踐解決方案:
使用您自己的服務,這可以保證您的實體在使用Objectify之前已注冊,但不一定會影響應用程序啟動以查看不訪問數據存儲區的請求。
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
public class OfyService {
static {
ObjectifyService.register(User.class);
}
public static Objectify ofy() {
return ObjectifyService.begin();//prior to v.4.0 use .begin() ,
//since v.4.0 use 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 );
}
ps,你也看看物化的最佳實踐
http://code.google.com/p/objectify-appengine/wiki/BestPractices
我使用@Entity
注釋, Reflections庫和運行時注冊,對我的任何應用程序的啟動時間沒有重大影響,因為所有信息都是在編譯/構建時收集的。
package com.vertigrated.servlet;
import com.google.appengine.api.ThreadManager;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Entity;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* This class processes the classpath for classes with the @Entity or @Subclass annotations from Objectify
* and registers them with the ObjectifyFactory, it is multi-threaded uses a prebuilt list of classes to process
* created by the Reflections library at compile time and works very fast!
*/
public class ObjectifyLoaderContextListener implements ServletContextListener
{
private static final Logger L = LoggerFactory.getLogger(ObjectifyLoaderContextListener.class);
private final Set<Class<?>> entities;
public ObjectifyLoaderContextListener()
{
this.entities = new HashSet<>();
}
@Override
public void contextInitialized(@Nonnull final ServletContextEvent sce)
{
final ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setUrls(ClasspathHelper.forPackage(""));
final ExecutorService es = Executors.newCachedThreadPool(ThreadManager.currentRequestThreadFactory());
cb.setExecutorService(es);
final Reflections r = new Reflections(cb);
this.entities.addAll(r.getTypesAnnotatedWith(Entity.class));
es.shutdown();
final ObjectifyFactory of = ObjectifyService.factory();
for (final Class<?> cls : this.entities)
{
of.register(cls);
L.debug("Registered {} with Objectify", cls.getName());
}
}
@Override
public void contextDestroyed(@Nonnull final ServletContextEvent sce)
{
/* this is intentionally empty */
}
}
基於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();
}
}
然后每當我想使用它時,我只是注入這樣的服務:
@Autowired
@Qualifier("objectifyService")
OfyService objectifyService;
然后我像這樣使用它:
objectifyService.ofy().save().entity(user).now();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.