Google Guice桌面应用程序 - 如何使其工作? [英] Google Guice desktop application - how to make it work?

查看:134
本文介绍了Google Guice桌面应用程序 - 如何使其工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网络应用中使用过Guice而没有任何问题,我想在桌面应用中使用它。我当然缺少一件事 - 某种方式告诉我的应用程序如何绑定所有内容并知道什么是什么。在Web应用程序中,我在Application类中声明了这一点,我应该如何在桌面应用程序中执行此操作?

I have used Guice in my web app without problems and I wanted to use it in desktop app. I am certainly missing one thing - some way to tell my app how to bind everything and know what is what. In web app, I had declaration for that in Application class, how should I do it in my desktop app?

这是我正在使用的相关代码:

Here is relevant code that I am using:

public class GuiceModule extends AbstractModule
{
   @Override
   protected void configure()
   {
   // Enable per-request-thread PersistenceManager injection.
   install(new PersistenceManagerFilter.GuiceModule());
   // Business object bindings go here.
   bind(ProjectQueries.class).to(JdoProjectQueries.class);
   bind(new TypeLiteral<Repository<Project>>() { }).to(JdoProjectRepository.class);
 }

我的主要课程:

@Inject
public Repository<Project> projectRepo;

public void createNewProject() {
   ...
   projectRepo.persist(newProject);
}

我当然正在使用projectRepo.persist(newProject);

I am of course getting on projectRepo.persist(newProject);

那么,我还需要做些什么才能让它发挥作用?

So, what else do I have to do to make it work?

编辑:

好的,那个部分现在正常工作,谢谢:)似乎我需要做更多的事情来使持久性工作那样。

Ok, that part work now, thanks :) It seems that I need to do a bit more though to make persistence work that way.

我现在在这里获得NPE:

I am getting NPE here now:

public void persist(T entity) 
{ 
pmProvider.get().makePersistent(entity); 
} 

get()在此返回null

get() returns null here

看起来像
install(new PersistenceManagerFilter.GuiceModule());
是不够的。我需要做什么? My Repository类以以下内容开头:

It looks like install(new PersistenceManagerFilter.GuiceModule()); is not enough. What do I need to do? My Repository class starts with:

public abstract class JdoRepository<T> implements Repository<T> { 
  private final Class<T> clazz; 
  private final Provider<PersistenceManager> pmProvider; 
  protected JdoRepository(Class<T> clazz, Provider<PersistenceManager> pmProvider)    {       this.clazz = clazz; this.pmProvider = pmProvider; 
} 

在我的PMF我有:

public static class GuiceModule extends AbstractModule { 

  @Override protected void configure() { 
    bind(PersistenceManager.class).toProvider(new Provider<PersistenceManager>() {
      public PersistenceManager get() { 
         return PersistenceManagerFilter.pm.get(); 
         } 
      }); 
    } 
   }


推荐答案

创建带有main方法的 Bootstrap 类。

Create Bootstrap class with main method.

将当前的静态主方法代码移动到非静态方法代码。例如 Application#run

Move your current static main method code to non-static one. For example Application#run.

Bootstrap中创建main方法 class:

public static void main(String[] args) {
    Injector injector = Guice.createInjector(new GuiceModule())
    Application app = injector.getInstance(Application.class);
    app.run();
}

运行 Bootstrap 类。

这篇关于Google Guice桌面应用程序 - 如何使其工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆