英文:
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]
你有任何想法如何解决这个问题吗?或者使用接口注入并通过参数/注解指定具体类是错误的想法吗?
我已经阅读并尝试了以下文章:
一些官方文档:
- Quarkus - 上下文和依赖注入 https://quarkus.io/guides/cdi-reference
- JSR 365: Java的上下文和依赖注入 2.0 https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#default_bean_discovery
- 利用CDI和EJB 3.1实现按需接口 https://www.oracle.com/technical-resources/articles/java/intondemand.html
- 23.7 注入Beans - Java Platform,企业版:Java EE教程(第7版) https://docs.oracle.com/javaee/7/tutorial/cdi-basic007.htm
其他博客和Stack Overflow 帖子:
- java - 如何注入JpaRepository的实现 - Stack Overflow https://stackoverflow.com/questions/60601330/how-inject-implementation-of-jparepository
- java - 如何注入实现相同接口的两个不同类的实例? - Stack Overflow https://stackoverflow.com/questions/55618095/how-to-inject-two-instances-of-two-different-classes-which-implement-the-same-in
- Java EE 上下文和依赖注入 @Qualifier https://memorynotfound.com/context-dependency-injection-qualifier/
英文:
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:
- Quarkus - Contexts and Dependency Injection https://quarkus.io/guides/cdi-reference
- JSR 365: Contexts and Dependency Injection for Java 2.0 https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#default_bean_discovery
- Interfaces on Demand with CDI and EJB 3.1 https://www.oracle.com/technical-resources/articles/java/intondemand.html
- 23.7 Injecting Beans - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7) https://docs.oracle.com/javaee/7/tutorial/cdi-basic007.htm
The other blogs and SOs:
- java - how inject implementation of JpaRepository - Stack Overflow https://stackoverflow.com/questions/60601330/how-inject-implementation-of-jparepository
- java - How to inject two instances of two different classes which implement the same interface? - Stack Overflow https://stackoverflow.com/questions/55618095/how-to-inject-two-instances-of-two-different-classes-which-implement-the-same-in
- Java EE Context and Dependency Injection @Qualifier https://memorynotfound.com/context-dependency-injection-qualifier/
答案1
得分: 8
我的猜测是您需要向您的 ProductStockDummyRepository
添加作用域注解。可能是 @Singleton
或者 @ApplicationScoped
。
英文:
My guess is that you need to add a scope annotation to your ProductStockDummyRepository
. Probably either @Singleton
or @ApplicationScoped
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论