绑定错误:未绑定实现 - Guice

Error in binding: No implementation was bound - 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.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

所以,基本上,代码的结构如下:

实体: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);
}
}

• 持久存储 定义通用数据类型 E 的创建、更新和删除 • FooResource 调用 FooService

的客户端应用程序

我已经在

中实现了绑定

• FooModule

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

}

}

我该如何解决这个问题?

在 PersistenceModule 中,您无需注释即可绑定 PersistenceStore。在 FooService 的构造函数中,您要求使用 @Named("fooPersistence").

注释的依赖项

要么在绑定和注入点中使用注释,要么将其删除。

顺便说一句: 如果有机会,更喜欢 onami 坚持。它修复了 guice persist(已废弃)中的一些已知错误。