English 中文(简体)
Guice Creation Exception
原标题:
  • 时间:2009-11-19 22:32:58
  •  标签:
  • guice

I am trying to get Guice working with Struts, Hibernate and Quartz scheduler. When I deploy my application under Tomcat, I get the following error -

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)

And the code

@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();
      }
   }

And the 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>

Can anyone please shed some light on why this application is crashing? I have inherited this code from someone and I don t fully understand how the injection is happening using Guice.

I am suspecting it has to do with any jars or configuration problems?

Any help is greatly appreciated,

Thanks

问题回答

I have inherited this code from someone and I don t fully understand how the injection is happening using Guice.

The details are described on the Guice wiki. But grossly simplified:

The GuiceFilter intercepts all calls and allows Guice to redirect them to Guice injected objects.

A user-built class extending GuiceServletContextListener returns a correctly configured Injecter that Guice will create your objects with. This is set in your XML file as a listener.

This uses normal Modules and one or more ServletModules. The latter allow for a code-based configuration of servlets, filters and listeners.


If was to guess at your problem then the GuiceServletContextListener does not contain a reference to ManagerModule.





相关问题
What is the best way to use Guice and JMock together?

I have started using Guice to do some dependency injection on a project, primarily because I need to inject mocks (using JMock currently) a layer away from the unit test, which makes manual injection ...

Google Guice vs. PicoContainer for Dependency Injection

My team is researching dependency injection frameworks and is trying to decide between using Google-Guice and PicoContainer. We are looking for several things in our framework: A small code ...

Guice and JSF 2

I m trying to use Guice to inject properties of a JSF managed bean. This is all running on Google App Engine (which may or may not be important) I ve followed the instructions here: http://code....

JSR 330 and Guice interoperability

Does anybody have experience with JSR 330 vs Guice? From what I gather Guice is not an implementation of JSR 330 but if it is anything like Hibernate and JPA the implementation supports a bunch of ...

Alternative to dozer for bean mapping? [closed]

I am trying to figure out an easy way to map DTOs to entities without the boiler-plate code. While I was thinking of using dozer it appears to require a lot of xml configuration. Has anybody seen a ...

Guice Creation Exception

I am trying to get Guice working with Struts, Hibernate and Quartz scheduler. When I deploy my application under Tomcat, I get the following error - Nov 19, 2009 2:11:26 PM com.google.inject.struts2....

热门标签