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

huangapple go评论85阅读模式
英文:

Dependency injection with @Inject and Interface in Quarkus

问题

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

以下是我的代码部分。

UseCase 类:

@ApplicationScoped
public class ProductStockCheckUseCase {
    @Inject
    @Named("dummy")
    ProductStockRepository repo;

    public int checkProductStock() {
        ProductStock stock = repo.findBy("");
        return stock.getCount();
    }
}

Repository 接口:

public interface ProductStockRepository {
    public ProductStock findBy(String productId);
}

Repository 实现:

@Named("dummy")
public class ProductStockDummyRepository implements ProductStockRepository {

    public ProductStock findBy(final String productId) {
        final ProductStock productStock = new ProductStock();
        return productStock;
    }
}

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

dependencies {
    implementation 'io.quarkus:quarkus-resteasy'
    implementation 'io.quarkus:quarkus-arc'
    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")

    testImplementation 'io.quarkus:quarkus-junit5'
    testImplementation 'io.rest-assured:rest-assured'
}

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

Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type ProductStockRepository and qualifiers [@Named(value = "dummy")]
        - java member: ProductStockCheckUseCase#repo
        - 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:

@ApplicationScoped
public class ProductStockCheckUseCase {
    @Inject
    @Named("dummy")
    ProductStockRepository repo;

    public int checkProductStock() {
        ProductStock stock = repo.findBy("");
        return stock.getCount();
    }
}

Repository Interface:

public interface ProductStockRepository {
    public ProductStock findBy(String productId);
}

Repository Implementation:

@Named("dummy")
public class ProductStockDummyRepository implements ProductStockRepository {

    public ProductStock findBy(final String productId) {
        final ProductStock productStock = new ProductStock();
        return productStock;
    }
}

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

dependencies {
    implementation 'io.quarkus:quarkus-resteasy'
    implementation 'io.quarkus:quarkus-arc'
    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")

    testImplementation 'io.quarkus:quarkus-junit5'
    testImplementation 'io.rest-assured:rest-assured'
}

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

Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type ProductStockRepository and qualifiers [@Named(value = "dummy")]
        - java member: ProductStockCheckUseCase#repo
        - 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://go.coder-hub.com/63412802.html
匿名

发表评论

匿名网友

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

确定