簡體   English   中英

綁定錯誤:沒有綁定任何實現 - 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);
}
}

•PersistentStore為通用數據類型定義創建,更新和刪除E•FooResource客戶端應用程序調用FooService

我已經實現了綁定

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

}

}

我該如何解決這個問題?

在PersistenceModule中,您可以在不使用注釋的情況下綁定PersistenceStore。 在FooService的構造函數中,您要求使用@Named(“fooPersistence”)注釋的依賴項。

在結合點和注入點都使用注釋或刪除它們。

順便說一句:如果你有機會,首選onami仍然存在。 它修復了guice persist中的一些已知錯誤(被遺棄)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM