绑定错误:未绑定任何实现-Guice [英] Error in binding: No implementation was bound - Guice
问题描述
我正在尝试使用Guice解决依赖性.我已经通读了教程和示例,但仍然无法弄清楚为什么会继续出现此错误:
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:
实体:foo
•fooService
• 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 为通用数据类型E定义创建,更新和删除 •Foo资源 调用FooService的客户端应用程序
• PersistentStore defines create, update and delete for generic datatype E • FooResource client application which calls FooService
我已经在
•FooModule
• FooModule
public class FooModuleimplements Module {
public void configure(final Binder binder) {
binder.bind(FooResource.class);
}
}
•PersistenceModule
• 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>() {
});
}
}
如何解决此问题?
推荐答案
在PersistenceModule中,可以绑定没有注释的PersistenceStore.在FooService的构造函数中,您要求使用@Named("fooPersistence")注释的依赖项.
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.
顺便说一句: 如果您有机会,请优先选择onami.它修复了guice持久性(已被放弃)中的一些已知错误.
By the way: Prefere onami persist if you have the chance. It fixes some known bugs in guice persist (which is abandoned).
这篇关于绑定错误:未绑定任何实现-Guice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!