1

我正在尝试让 Guice 与 Struts、Hibernate 和 Quartz 调度程序一起工作。当我在 Tomcat 下部署我的应用程序时,我收到以下错误 -

Nov 19, 2009 2:11:26 PM com.google.inject.struts2.GuiceObjectFactory buildBean
INFO: Creating injector...
com.google.inject.CreationException: Guice configuration errors:

1) Error at com.sample.common.entity.PersistenceServiceImpl.scheduler(PersistenceServiceImpl.java:22):
  Error while injecting at com.sample.cacher.UiAction.persistenceService(UiAction.java:27): Error while   injecting at com.sample.cacher.UiAction.persistenceService(UiAction.java:27): Binding to    org.quartz.Scheduler not found. No bindings to that type were found.

2) Error at com.sample.common.entity.PersistenceServiceImpl.session(PersistenceServiceImpl.java:22):
  Error while injecting at com.sample.cacher.UiAction.persistenceService(UiAction.java:27): Error while injecting at com.sample.cacher.UiAction.persistenceService(UiAction.java:27): Binding to org.hibernate.Session not found. No bindings to that type were found.

2 error[s]
    at com.google.inject.BinderImpl.createInjector(BinderImpl.java:277)
    at com.google.inject.Guice.createInjector(Guice.java:79)
    at com.google.inject.Guice.createInjector(Guice.java:53)
    at com.google.inject.Guice.createInjector(Guice.java:43)

和代码

@ImplementedBy(PersistenceServiceImpl.class)
public interface PersistenceService {
public void save(JobInformation dataObject);

public void remove(String jobName, String jobGroup, Class jobClass);

public List getActiveJobsFor(String userName, Class clazz) throws Exception;
}

public class PersistenceServiceImpl implements PersistenceService 
{  
  @Inject
   private Session session;
  @Inject
   private Scheduler scheduler;
   ...
}

package com.sample.common.entity;
public class ManagerModule extends AbstractModule {
protected void configure() {
    bind(Session.class)
            .toProvider(SessionProvider.class);
    bind(Scheduler.class)
            .toProvider(SchedulerProvider.class);
}
}

public class UiAction extends ActionSupport implements PrincipalAware
{
   @Inject
   private PersistenceService persistenceService;
   ....

    public String doSave() throws ParseException {


        persistenceService.save(data);

       return  doList();
      }
   }

和 web.xml

<filter>
    <filter-name>guice</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>guice</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

谁能解释一下为什么这个应用程序崩溃了?我从某人那里继承了这段代码,但我不完全理解如何使用 Guice 进行注入。

我怀疑它与任何罐子或配置问题有关?

任何帮助是极大的赞赏,

谢谢

4

1 回答 1

2

我从某人那里继承了这段代码,但我不完全理解如何使用 Guice 进行注入。

详细信息在Guice wiki上进行了描述。但大大简化:

拦截所有调用并允许 Guice将GuiceFilter它们重定向到 Guice 注入对象。

用户构建的扩展类GuiceServletContextListener返回正确配置Injecter,Guice 将使用它创建对象。这在您的 XML 文件中设置为侦听器。

这使用 normal Modules 和一个或多个ServletModules。后者允许对servlet、过滤器和侦听器进行基于代码的配置


如果要猜测您的问题,则GuiceServletContextListener不包含对ManagerModule.

于 2009-11-20T11:39:58.017 回答