问题
I’m trying to use Guice to solve dependency. I’ve read through tutorials and examples, but I still can’t figure out why I keep getting this error:
No implementation for com.edit.owl.persistence.PersistentStore<com.edit.common.domain.Foo> annotated with @com.google.inject.name.Named(value=FooPersistence) was bound. while locating com.edit.owl.persistence.PersistentStore<com.edit.common.domain.Foo> annotated with @com.google.inject.name.Named(value=FooPersistence)
for parameter 0 at com.edit.owl.service.FooService.<init>(FooService.java:17)while locating com.edit.owl.service.FooService
for parameter 0 at com.edit.owl.resource.DistributionListResource.<init>(ListResource.java:36)while locating com.edit.owl.resource.ListResource
So, basically, the structure of the code is as follow:
Entity: foo
• fooService
@Singleton
public class FooService implements PersistentStore<Foo> {
private final PersistentStore<Foo> fooDAO;
@Inject
public FooService (@Named("fooPersistence")PersistentStore<Foo> fooRepository) {
this.fooDAO = fooRepository;
}
@Override
@Transactional
public Foo create(Foo foo) {
return fooDAO .create(foo);
}
@Override
@Transactional
public Foo update(Foo foo) {
return fooDAO.update(foo);
}
@Override
@Transactional
public Foo findById(Long Id) {
return fooDAO.findById(Id);
}
}
• PersistentStore defines create, update and delete for generic datatype E • FooResource client application which calls FooService
I have implemented binding in
• FooModule
public class FooModuleimplements Module {
public void configure(final Binder binder) {
binder.bind(FooResource.class);
}
}
• PersistenceModule
public class PersistenceModule extends AbstractModule {
private final String persistenceUnit;
public PersistenceModule(String persistenceUnit) {
this.persistenceUnit = persistenceUnit;
}
@Override
protected void configure() {
install(new JpaPersistModule(persistenceUnit));
bind(JpaInitializer.class).asEagerSingleton();
bind(new TypeLiteral<PersistentStore<Foo>>() {
}).to(new TypeLiteral<Foo>() {
});
}
}
How can I fix this problem?
回答1:
In the PersistenceModule you bind the PersistenceStore without annotation. In the constructor of the FooService you ask for a dependency annotated with @Named("fooPersistence").
Either use the annotations in both the binding and the injection point or remove them.
By the way: Prefere onami persist if you have the chance. It fixes some known bugs in guice persist (which is abandoned).
来源:https://stackoverflow.com/questions/30611635/error-in-binding-no-implementation-was-bound-guice