依赖注入使用 @Inject 和接口在 Quarkus 中

huangapple 未分类评论101阅读模式
英文:

Dependency injection with @Inject and Interface in Quarkus

问题

我正在尝试使用Quarkus 1.6.1.Final和OpenJDK 11解决使用Repository模式的依赖注入问题。我希望通过接口进行注入,并为它们提供一些参数(例如 @Named@Qualifier )来指定具体的类,但目前我遇到了 UnsatisfiedResolutionException 错误,并且不确定如何修复它。

以下是我的代码部分。

UseCase 类:

  1. @ApplicationScoped
  2. public class ProductStockCheckUseCase {
  3. @Inject
  4. @Named("dummy")
  5. ProductStockRepository repo;
  6. public int checkProductStock() {
  7. ProductStock stock = repo.findBy("");
  8. return stock.getCount();
  9. }
  10. }

Repository 接口:

  1. public interface ProductStockRepository {
  2. public ProductStock findBy(String productId);
  3. }

Repository 实现:

  1. @Named("dummy")
  2. public class ProductStockDummyRepository implements ProductStockRepository {
  3. public ProductStock findBy(final String productId) {
  4. final ProductStock productStock = new ProductStock();
  5. return productStock;
  6. }
  7. }

这是我的 build.gradle 的依赖部分:

  1. dependencies {
  2. implementation 'io.quarkus:quarkus-resteasy'
  3. implementation 'io.quarkus:quarkus-arc'
  4. implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
  5. testImplementation 'io.quarkus:quarkus-junit5'
  6. testImplementation 'io.rest-assured:rest-assured'
  7. }

当我运行这个项目(例如 ./gradlew assemble./gradlew quarkusDev ),我遇到了以下错误:

  1. Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type ProductStockRepository and qualifiers [@Named(value = "dummy")]
  2. - java member: ProductStockCheckUseCase#repo
  3. - declared on CLASS bean [types=[ProductStockCheckUseCase, java.lang.Object], qualifiers=[@Default, @Any], target=ProductStockCheckUseCase]

你有任何想法如何解决这个问题吗?或者使用接口注入并通过参数/注解指定具体类是错误的想法吗?

我已经阅读并尝试了以下文章:

一些官方文档:

其他博客和Stack Overflow 帖子:

英文:

I'm trying to resolve dependency injection with Repository Pattern using Quarkus 1.6.1.Final and OpenJDK 11.
I want to achieve Inject with Interface and give them some argument(like @Named or @Qualifier ) for specify the concrete class, but currently I've got UnsatisfiedResolutionException and not sure how to fix it.

Here is the my portion of code.

UseCase class:

  1. @ApplicationScoped
  2. public class ProductStockCheckUseCase {
  3. @Inject
  4. @Named("dummy")
  5. ProductStockRepository repo;
  6. public int checkProductStock() {
  7. ProductStock stock = repo.findBy("");
  8. return stock.getCount();
  9. }
  10. }

Repository Interface:

  1. public interface ProductStockRepository {
  2. public ProductStock findBy(String productId);
  3. }

Repository Implementation:

  1. @Named("dummy")
  2. public class ProductStockDummyRepository implements ProductStockRepository {
  3. public ProductStock findBy(final String productId) {
  4. final ProductStock productStock = new ProductStock();
  5. return productStock;
  6. }
  7. }

And here is a portion of my build.gradle's dependencies:

  1. dependencies {
  2. implementation 'io.quarkus:quarkus-resteasy'
  3. implementation 'io.quarkus:quarkus-arc'
  4. implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
  5. testImplementation 'io.quarkus:quarkus-junit5'
  6. testImplementation 'io.rest-assured:rest-assured'
  7. }

When I run this (e.g. ./gradlew assemble or ./gradlew quarkusDev ), I've got the following errors:

  1. Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type ProductStockRepository and qualifiers [@Named(value = "dummy")]
  2. - java member: ProductStockCheckUseCase#repo
  3. - declared on CLASS bean [types=[ProductStockCheckUseCase, java.lang.Object], qualifiers=[@Default, @Any], target=ProductStockCheckUseCase]

Do you have any ideas how to fix this? or is it wrong idea to implement this kind of interface injection and specify the concrete class with argument/annotation?

I've read and tried the following articles:

Some official docs:

The other blogs and SOs:

答案1

得分: 8

我的猜测是您需要向您的 ProductStockDummyRepository 添加作用域注解。可能是 @Singleton 或者 @ApplicationScoped

英文:

My guess is that you need to add a scope annotation to your ProductStockDummyRepository. Probably either @Singleton or @ApplicationScoped.

huangapple
  • 本文由 发表于 2020年8月14日 20:30:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63412802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定