A wild guess... does the class "this.persister" refers to have any methods
or fields annotated with @Inject?  Guice is probably trying to Inject them.
 Without thinking too much about the relative merits of this design, one
possible workaround is to bind it using
bind(Persister.class).toProvider(Providers.of(this.persister)).

sam

On Fri, May 14, 2010 at 12:31 AM, yurique <[email protected]> wrote:

> Hi!
>
> I've faced the following problem.
>
> I am using Guice within OSGI (though I'm not sure if it is relevant to
> the problem itself).
>
> I have one bundle - Persister, within it I configure Guice and then
> inject members like this:
>
>                Guice.createInjector(new AbstractModule() {
>                        @Override
>                        protected void configure() {
>
>  install(PersistenceModule.create("SOME_PERSISTENCE_UNIT"));
>                        }
>                }).injectMembers(this);
>
> PersistenceModule.create() initializes a module for warp-persist and
> returns it. Actually, this code works just fine - everything gets
> injected correctly.
>
> This bundle then exports this guice-initialized object as an OSGI
> service ("Persister").
>
> Another bundle (Client) uses this service. First it is passed with a
> "Persister" service instance:
>
>        protected void setPersister(Persister persister) {
>                this.persister = persister;
>        }
>
> This works fine as well. After that the Client is activated:
>
>        protected void activate(ComponentContext context) {
>                Guice.createInjector(new AbstractModule() {
>                        @Override
>                        protected void configure() {
>
>  bind(Persister.class).toInstance(this.persister); //
> this.persister is initialized earlier by the setter method mentioned
> above
>                        }
>                }).injectMembers(this);
>        }
>
> injectMembers() initialized needed fields, and some of them in turn
> have fields like this:
>
>        @Inject
>        private Persister persister;
>
> I expected that those fields would just be set according to the
> "bind(Persister.class).toInstance(this.persister)" configuration
> instruction. But instead I get exceptions like the following:
>
> 1) No implementation for javax.persistence.EntityManager was bound.
>  while locating
> com.google.inject.Provider<javax.persistence.EntityManager>
>    for field at persister.impl.RequestDAO.em(RequestDAO.java:15)
>  while locating persister.impl.RequestDAO
>    for field at
> persister.impl.PersisterComponent.requestDAO(PersisterComponent.java:
> 42)
>  at client.ClientComponent$1.configure(ClientComponent.java:47)
>
> 2) No implementation for javax.persistence.EntityManager was bound.
>  while locating
> com.google.inject.Provider<javax.persistence.EntityManager>
>    for field at persister.impl.ResponseDAO.em(ResponseDAO.java:15)
>  while locating persister.impl.ResponseDAO
>    for field at
> persister.impl.PersisterComponent.responseDAO(PersisterComponent.java:
> 41)
>  at client.ClientComponent$1.configure
>
> 2 errors
>        at
>
> com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:
> 354)
>        at
>
> com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:
> 152)
>        at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
>        at com.google.inject.Guice.createInjector(Guice.java:92)
>        at com.google.inject.Guice.createInjector(Guice.java:69)
>        at com.google.inject.Guice.createInjector(Guice.java:59)
>        at client.ClientComponent.activate
>        ... 36 more
>
> I can't figure out what Guice is trying to do. Why would it need
> bindings for EntityManager which is only referenced by objects that
> are already successfully built before (using a different module)?
>
> P.S. I'll try to summarize the above:
>
> [Persister bundle]: configure-guice, inject members, export built
> object as a service (works fine)
> [Client bundle]: consume the above service, save it to a field (ok),
> configure guice and bind Persister.class to that object, create an
> injector - fails
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to