Java 绑定中出错:未绑定任何实现-Guice
我正在尝试使用Guice解决依赖性问题。我已经通读了教程和示例,但我仍然不明白为什么我会不断出现这个错误:Java 绑定中出错:未绑定任何实现-Guice,java,guice,Java,Guice,我正在尝试使用Guice解决依赖性问题。我已经通读了教程和示例,但我仍然不明白为什么我会不断出现这个错误: 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.ed
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
•持久性模块
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扩展了AbstractModule{
私有最终字符串持久化单元;
公共持久性模块(字符串持久性单元){
this.persistenceUnit=persistenceUnit;
}
@凌驾
受保护的void configure(){
安装(新的JpaPersistModule(persistenceUnit));
bind(jpainInitializer.class).asagersingleton();
绑定(新的TypeLiteral(){
}).to(新的TypeLiteral(){
});
}
}
如何解决此问题?在PersistenceModule中,您绑定PersistenceStore而不添加注释。在FooService的构造函数中,您要求使用@Named(“fooPersistence”)注释依赖项 要么在绑定和注入点中使用注释,要么删除它们 顺便说一下:
如果你有机会的话,你更愿意坚持下去。它修复了guice persist中的一些已知错误(已放弃)。我在FooResource类中放置了以下代码行:@InjectPublic FooResource(Foo Service fooService){this.fooService=fooService;}
public class FooModuleimplements Module {
public void configure(final Binder binder) {
binder.bind(FooResource.class);
}
}
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>() {
});
}