Guice创建例外

时间:2009-11-19 22:32:59

标签: guice

我试图让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如何进行注入。

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

非常感谢任何帮助,

谢谢

1 个答案:

答案 0 :(得分:2)

  

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

详情见Guice wiki。但非常简化:

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

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

这使用普通Module和一个或多个ServletModule。后者允许code-based configuration servlet,过滤器和监听器。


如果要猜测您的问题,那么GuiceServletContextListener不包含对ManagerModule的引用。